blob: 09a1f5279c639fa6d363798ee9a1153540e851d3 [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;
17 // explicitly specify a trim rowbytes, so we have no padding on each row
18 bm.setConfig(SkBitmap::kARGB_8888_Config, DIMENSION, DIMENSION, DIMENSION*4);
19 bm.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000020 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comee068aa2011-12-21 19:36:21 +000021
22 SkCanvas canvas(bm);
23 SkPaint p(paint);
24 p.setColor(SK_ColorWHITE);
25
26 canvas.drawPath(path, p);
27
28 size_t count = DIMENSION * DIMENSION;
29 const SkPMColor* ptr = bm.getAddr32(0, 0);
30
caryclark@google.com42639cd2012-06-06 12:03:39 +000031 SkPMColor andValue = ~0U;
reed@google.comee068aa2011-12-21 19:36:21 +000032 SkPMColor orValue = 0;
33 for (size_t i = 0; i < count; ++i) {
34 SkPMColor c = ptr[i];
35 andValue &= c;
36 orValue |= c;
37 }
38
39 // success means we drew everywhere or nowhere (depending on shouldDraw)
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000040 bool success = shouldDraw ? (~0U == andValue) : (0 == orValue);
reed@google.comee068aa2011-12-21 19:36:21 +000041
42 if (!success) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000043 const char* str;
reed@google.comee068aa2011-12-21 19:36:21 +000044 if (shouldDraw) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000045 str = "Path expected to draw everywhere, but didn't. ";
reed@google.comee068aa2011-12-21 19:36:21 +000046 } else {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000047 str = "Path expected to draw nowhere, but did. ";
reed@google.comee068aa2011-12-21 19:36:21 +000048 }
halcanary@google.coma9325fa2014-01-10 14:58:10 +000049 ERRORF(reporter, "%s style[%d] cap[%d] join[%d] antialias[%d]"
50 " filltype[%d] ptcount[%d]", str, paint.getStyle(),
51 paint.getStrokeCap(), paint.getStrokeJoin(),
52 paint.isAntiAlias(), path.getFillType(), path.countPoints());
reed@google.comee068aa2011-12-21 19:36:21 +000053// uncomment this if you want to step in to see the failure
54// canvas.drawPath(path, p);
55 }
56}
57
58static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw) {
59 static const SkPaint::Cap gCaps[] = {
60 SkPaint::kButt_Cap,
61 SkPaint::kRound_Cap,
62 SkPaint::kSquare_Cap
63 };
64 static const SkPaint::Join gJoins[] = {
65 SkPaint::kMiter_Join,
66 SkPaint::kRound_Join,
67 SkPaint::kBevel_Join
68 };
69 static const SkPaint::Style gStyles[] = {
70 SkPaint::kFill_Style,
71 SkPaint::kStroke_Style,
72 SkPaint::kStrokeAndFill_Style
73 };
74 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
75 for (size_t join = 0; join < SK_ARRAY_COUNT(gJoins); ++join) {
76 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
77 SkPaint paint;
78 paint.setStrokeWidth(SkIntToScalar(10));
79
80 paint.setStrokeCap(gCaps[cap]);
81 paint.setStrokeJoin(gJoins[join]);
82 paint.setStyle(gStyles[style]);
83
84 paint.setAntiAlias(false);
85 drawAndTest(reporter, path, paint, shouldDraw);
86 paint.setAntiAlias(true);
87 drawAndTest(reporter, path, paint, shouldDraw);
88 }
89 }
90 }
91}
92
93#define CX (SkIntToScalar(DIMENSION) / 2)
94#define CY (SkIntToScalar(DIMENSION) / 2)
95
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000096static void make_empty(SkPath*) {}
reed@google.com21e0bc22011-12-21 20:26:01 +000097static void make_M(SkPath* path) { path->moveTo(CX, CY); }
98static void make_MM(SkPath* path) { path->moveTo(CX, CY); path->moveTo(CX, CY); }
99static void make_MZM(SkPath* path) { path->moveTo(CX, CY); path->close(); path->moveTo(CX, CY); }
100static void make_L(SkPath* path) { path->moveTo(CX, CY); path->lineTo(CX, CY); }
101static void make_Q(SkPath* path) { path->moveTo(CX, CY); path->quadTo(CX, CY, CX, CY); }
102static 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 +0000103
104/* Two invariants are tested: How does an empty/degenerate path draw?
105 * - if the path is drawn inverse, it should draw everywhere
106 * - if the path is drawn non-inverse, it should draw nowhere
107 *
108 * Things to iterate on:
109 * - path (empty, degenerate line/quad/cubic w/ and w/o close
110 * - paint style
111 * - path filltype
112 * - path stroke variants (e.g. caps, joins, width)
113 */
114static void test_emptydrawing(skiatest::Reporter* reporter) {
115 static void (*gMakeProc[])(SkPath*) = {
reed@google.com21e0bc22011-12-21 20:26:01 +0000116 make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C
reed@google.comee068aa2011-12-21 19:36:21 +0000117 };
118 static SkPath::FillType gFills[] = {
119 SkPath::kWinding_FillType,
120 SkPath::kEvenOdd_FillType,
121 SkPath::kInverseWinding_FillType,
122 SkPath::kInverseEvenOdd_FillType
123 };
124 for (int doClose = 0; doClose < 2; ++doClose) {
125 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProc); ++i) {
126 SkPath path;
127 gMakeProc[i](&path);
128 if (doClose) {
129 path.close();
130 }
131 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
132 path.setFillType(gFills[fill]);
133 bool shouldDraw = path.isInverseFillType();
134 iter_paint(reporter, path, shouldDraw);
135 }
136 }
137 }
138}
139
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000140DEF_TEST(EmptyPath, reporter) {
reed@google.comee068aa2011-12-21 19:36:21 +0000141 test_emptydrawing(reporter);
142}