blob: 060ef8d23724b5433295c10869008f832850c278 [file] [log] [blame]
reed@google.comee068aa2011-12-21 19:36:21 +00001/*
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
reed@google.comee068aa2011-12-21 19:36:21 +00008#include "SkCanvas.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "SkPath.h"
10#include "Test.h"
reed@google.comee068aa2011-12-21 19:36:21 +000011
reed@google.comee068aa2011-12-21 19:36:21 +000012#define DIMENSION 32
13
14static void drawAndTest(skiatest::Reporter* reporter, const SkPath& path,
15 const SkPaint& paint, bool shouldDraw) {
16 SkBitmap bm;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000017 bm.allocN32Pixels(DIMENSION, DIMENSION);
18 SkASSERT(DIMENSION*4 == bm.rowBytes()); // ensure no padding on each row
junov@google.comdbfac8a2012-12-06 21:47:40 +000019 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comee068aa2011-12-21 19:36:21 +000020
21 SkCanvas canvas(bm);
22 SkPaint p(paint);
23 p.setColor(SK_ColorWHITE);
24
25 canvas.drawPath(path, p);
26
27 size_t count = DIMENSION * DIMENSION;
28 const SkPMColor* ptr = bm.getAddr32(0, 0);
29
caryclark@google.com42639cd2012-06-06 12:03:39 +000030 SkPMColor andValue = ~0U;
reed@google.comee068aa2011-12-21 19:36:21 +000031 SkPMColor orValue = 0;
32 for (size_t i = 0; i < count; ++i) {
33 SkPMColor c = ptr[i];
34 andValue &= c;
35 orValue |= c;
36 }
37
38 // success means we drew everywhere or nowhere (depending on shouldDraw)
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000039 bool success = shouldDraw ? (~0U == andValue) : (0 == orValue);
reed@google.comee068aa2011-12-21 19:36:21 +000040
41 if (!success) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000042 const char* str;
reed@google.comee068aa2011-12-21 19:36:21 +000043 if (shouldDraw) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000044 str = "Path expected to draw everywhere, but didn't. ";
reed@google.comee068aa2011-12-21 19:36:21 +000045 } else {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000046 str = "Path expected to draw nowhere, but did. ";
reed@google.comee068aa2011-12-21 19:36:21 +000047 }
halcanary@google.coma9325fa2014-01-10 14:58:10 +000048 ERRORF(reporter, "%s style[%d] cap[%d] join[%d] antialias[%d]"
49 " filltype[%d] ptcount[%d]", str, paint.getStyle(),
50 paint.getStrokeCap(), paint.getStrokeJoin(),
51 paint.isAntiAlias(), path.getFillType(), path.countPoints());
reed@google.comee068aa2011-12-21 19:36:21 +000052// uncomment this if you want to step in to see the failure
53// canvas.drawPath(path, p);
54 }
55}
56
caryclark6651a322015-09-09 13:20:49 -070057enum DrawCaps {
58 kDontDrawCaps,
59 kDrawCaps
60};
61
62static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw,
63 DrawCaps drawCaps) {
reed@google.comee068aa2011-12-21 19:36:21 +000064 static const SkPaint::Cap gCaps[] = {
65 SkPaint::kButt_Cap,
66 SkPaint::kRound_Cap,
67 SkPaint::kSquare_Cap
68 };
69 static const SkPaint::Join gJoins[] = {
70 SkPaint::kMiter_Join,
71 SkPaint::kRound_Join,
72 SkPaint::kBevel_Join
73 };
74 static const SkPaint::Style gStyles[] = {
75 SkPaint::kFill_Style,
76 SkPaint::kStroke_Style,
77 SkPaint::kStrokeAndFill_Style
78 };
79 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
80 for (size_t join = 0; join < SK_ARRAY_COUNT(gJoins); ++join) {
81 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
caryclark6651a322015-09-09 13:20:49 -070082 if (drawCaps && SkPaint::kButt_Cap != gCaps[cap]
83 && SkPaint::kFill_Style != gStyles[style]) {
84 continue;
85 }
86
reed@google.comee068aa2011-12-21 19:36:21 +000087 SkPaint paint;
88 paint.setStrokeWidth(SkIntToScalar(10));
89
90 paint.setStrokeCap(gCaps[cap]);
91 paint.setStrokeJoin(gJoins[join]);
92 paint.setStyle(gStyles[style]);
93
94 paint.setAntiAlias(false);
95 drawAndTest(reporter, path, paint, shouldDraw);
96 paint.setAntiAlias(true);
97 drawAndTest(reporter, path, paint, shouldDraw);
98 }
99 }
100 }
101}
102
103#define CX (SkIntToScalar(DIMENSION) / 2)
104#define CY (SkIntToScalar(DIMENSION) / 2)
105
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000106static void make_empty(SkPath*) {}
reed@google.com21e0bc22011-12-21 20:26:01 +0000107static void make_M(SkPath* path) { path->moveTo(CX, CY); }
108static void make_MM(SkPath* path) { path->moveTo(CX, CY); path->moveTo(CX, CY); }
109static void make_MZM(SkPath* path) { path->moveTo(CX, CY); path->close(); path->moveTo(CX, CY); }
110static void make_L(SkPath* path) { path->moveTo(CX, CY); path->lineTo(CX, CY); }
111static void make_Q(SkPath* path) { path->moveTo(CX, CY); path->quadTo(CX, CY, CX, CY); }
112static void make_C(SkPath* path) { path->moveTo(CX, CY); path->cubicTo(CX, CY, CX, CY, CX, CY); }
reed@google.comee068aa2011-12-21 19:36:21 +0000113
114/* Two invariants are tested: How does an empty/degenerate path draw?
115 * - if the path is drawn inverse, it should draw everywhere
116 * - if the path is drawn non-inverse, it should draw nowhere
117 *
118 * Things to iterate on:
119 * - path (empty, degenerate line/quad/cubic w/ and w/o close
120 * - paint style
121 * - path filltype
122 * - path stroke variants (e.g. caps, joins, width)
123 */
124static void test_emptydrawing(skiatest::Reporter* reporter) {
125 static void (*gMakeProc[])(SkPath*) = {
reed@google.com21e0bc22011-12-21 20:26:01 +0000126 make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C
reed@google.comee068aa2011-12-21 19:36:21 +0000127 };
128 static SkPath::FillType gFills[] = {
129 SkPath::kWinding_FillType,
130 SkPath::kEvenOdd_FillType,
131 SkPath::kInverseWinding_FillType,
132 SkPath::kInverseEvenOdd_FillType
133 };
134 for (int doClose = 0; doClose < 2; ++doClose) {
135 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProc); ++i) {
136 SkPath path;
137 gMakeProc[i](&path);
138 if (doClose) {
139 path.close();
140 }
caryclark6651a322015-09-09 13:20:49 -0700141 /* zero length segments and close following moves draw round and square caps */
142 bool allowCaps = make_L == gMakeProc[i] || make_Q == gMakeProc[i]
143 || make_C == gMakeProc[i] || make_MZM == gMakeProc[i];
144 allowCaps |= SkToBool(doClose);
reed@google.comee068aa2011-12-21 19:36:21 +0000145 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
146 path.setFillType(gFills[fill]);
147 bool shouldDraw = path.isInverseFillType();
caryclark6651a322015-09-09 13:20:49 -0700148 iter_paint(reporter, path, shouldDraw, allowCaps ? kDrawCaps : kDontDrawCaps);
reed@google.comee068aa2011-12-21 19:36:21 +0000149 }
150 }
151 }
152}
153
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000154DEF_TEST(EmptyPath, reporter) {
reed@google.comee068aa2011-12-21 19:36:21 +0000155 test_emptydrawing(reporter);
156}