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