xref: /aosp_15_r20/external/skia/tests/EmptyPathTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2011 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/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathTypes.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkTypes.h"
16 #include "include/private/base/SkTo.h"
17 #include "tests/Test.h"
18 
19 #include <array>
20 #include <cstddef>
21 
22 #define DIMENSION   32
23 
drawAndTest(skiatest::Reporter * reporter,const SkPath & path,const SkPaint & paint,bool shouldDraw)24 static void drawAndTest(skiatest::Reporter* reporter, const SkPath& path,
25                         const SkPaint& paint, bool shouldDraw) {
26     SkBitmap bm;
27     bm.allocN32Pixels(DIMENSION, DIMENSION);
28     SkASSERT(DIMENSION*4 == bm.rowBytes()); // ensure no padding on each row
29     bm.eraseColor(SK_ColorTRANSPARENT);
30 
31     SkCanvas canvas(bm);
32     SkPaint p(paint);
33     p.setColor(SK_ColorWHITE);
34 
35     canvas.drawPath(path, p);
36 
37     size_t count = DIMENSION * DIMENSION;
38     const SkPMColor* ptr = bm.getAddr32(0, 0);
39 
40     SkPMColor andValue = ~0U;
41     SkPMColor orValue = 0;
42     for (size_t i = 0; i < count; ++i) {
43         SkPMColor c = ptr[i];
44         andValue &= c;
45         orValue |= c;
46     }
47 
48     // success means we drew everywhere or nowhere (depending on shouldDraw)
49     bool success = shouldDraw ? (~0U == andValue) : (0 == orValue);
50 
51     if (!success) {
52         const char* str;
53         if (shouldDraw) {
54             str = "Path expected to draw everywhere, but didn't. ";
55         } else {
56             str = "Path expected to draw nowhere, but did. ";
57         }
58         ERRORF(reporter, "%s style[%d] cap[%d] join[%d] antialias[%d]"
59                " filltype[%d] ptcount[%d]", str, paint.getStyle(),
60                paint.getStrokeCap(), paint.getStrokeJoin(),
61                paint.isAntiAlias(), (int)path.getFillType(), path.countPoints());
62 // uncomment this if you want to step in to see the failure
63 //        canvas.drawPath(path, p);
64     }
65 }
66 
67 enum DrawCaps {
68     kDontDrawCaps,
69     kDrawCaps
70 };
71 
iter_paint(skiatest::Reporter * reporter,const SkPath & path,bool shouldDraw,DrawCaps drawCaps)72 static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw,
73                        DrawCaps drawCaps) {
74     static const SkPaint::Cap gCaps[] = {
75         SkPaint::kButt_Cap,
76         SkPaint::kRound_Cap,
77         SkPaint::kSquare_Cap
78     };
79     static const SkPaint::Join gJoins[] = {
80         SkPaint::kMiter_Join,
81         SkPaint::kRound_Join,
82         SkPaint::kBevel_Join
83     };
84     static const SkPaint::Style gStyles[] = {
85         SkPaint::kFill_Style,
86         SkPaint::kStroke_Style,
87         SkPaint::kStrokeAndFill_Style
88     };
89     for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
90         for (size_t join = 0; join < std::size(gJoins); ++join) {
91             for (size_t style = 0; style < std::size(gStyles); ++style) {
92                 if (drawCaps && SkPaint::kButt_Cap != gCaps[cap]
93                         && SkPaint::kFill_Style != gStyles[style]) {
94                     continue;
95                 }
96 
97                 SkPaint paint;
98                 paint.setStrokeWidth(SkIntToScalar(10));
99 
100                 paint.setStrokeCap(gCaps[cap]);
101                 paint.setStrokeJoin(gJoins[join]);
102                 paint.setStyle(gStyles[style]);
103 
104                 paint.setAntiAlias(false);
105                 drawAndTest(reporter, path, paint, shouldDraw);
106                 paint.setAntiAlias(true);
107                 drawAndTest(reporter, path, paint, shouldDraw);
108             }
109         }
110     }
111 }
112 
113 #define CX  (SkIntToScalar(DIMENSION) / 2)
114 #define CY  (SkIntToScalar(DIMENSION) / 2)
115 
make_empty(SkPath *)116 static void make_empty(SkPath*) {}
make_M(SkPath * path)117 static void make_M(SkPath* path) { path->moveTo(CX, CY); }
make_MM(SkPath * path)118 static void make_MM(SkPath* path) { path->moveTo(CX, CY).moveTo(CX, CY); }
make_MZM(SkPath * path)119 static void make_MZM(SkPath* path) { path->moveTo(CX, CY).close().moveTo(CX, CY); }
make_L(SkPath * path)120 static void make_L(SkPath* path) { path->moveTo(CX, CY).lineTo(CX, CY); }
make_Q(SkPath * path)121 static void make_Q(SkPath* path) { path->moveTo(CX, CY).quadTo(CX, CY, CX, CY); }
make_C(SkPath * path)122 static void make_C(SkPath* path) { path->moveTo(CX, CY).cubicTo(CX, CY, CX, CY, CX, CY); }
123 
124 /*  Two invariants are tested: How does an empty/degenerate path draw?
125  *  - if the path is drawn inverse, it should draw everywhere
126  *  - if the path is drawn non-inverse, it should draw nowhere
127  *
128  *  Things to iterate on:
129  *  - path (empty, degenerate line/quad/cubic w/ and w/o close
130  *  - paint style
131  *  - path filltype
132  *  - path stroke variants (e.g. caps, joins, width)
133  */
test_emptydrawing(skiatest::Reporter * reporter)134 static void test_emptydrawing(skiatest::Reporter* reporter) {
135     static void (*gMakeProc[])(SkPath*) = {
136         make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C
137     };
138     static SkPathFillType gFills[] = {
139         SkPathFillType::kWinding,
140         SkPathFillType::kEvenOdd,
141         SkPathFillType::kInverseWinding,
142         SkPathFillType::kInverseEvenOdd
143     };
144     for (int doClose = 0; doClose < 2; ++doClose) {
145         for  (size_t i = 0; i < std::size(gMakeProc); ++i) {
146             SkPath path;
147             gMakeProc[i](&path);
148             if (doClose) {
149                 path.close();
150             }
151             /* zero length segments and close following moves draw round and square caps */
152             bool allowCaps = make_L == gMakeProc[i] || make_Q == gMakeProc[i]
153                     || make_C == gMakeProc[i] || make_MZM == gMakeProc[i];
154             allowCaps |= SkToBool(doClose);
155             for (size_t fill = 0; fill < std::size(gFills); ++fill) {
156                 path.setFillType(gFills[fill]);
157                 bool shouldDraw = path.isInverseFillType();
158                 iter_paint(reporter, path, shouldDraw, allowCaps ? kDrawCaps : kDontDrawCaps);
159             }
160         }
161     }
162 }
163 
DEF_TEST(EmptyPath,reporter)164 DEF_TEST(EmptyPath, reporter) {
165     test_emptydrawing(reporter);
166 }
167