reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 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 | |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "SkPath.h" |
| 11 | #include "Test.h" |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 12 | |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 13 | #define DIMENSION 32 |
| 14 | |
| 15 | static void drawAndTest(skiatest::Reporter* reporter, const SkPath& path, |
| 16 | const SkPaint& paint, bool shouldDraw) { |
| 17 | SkBitmap bm; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 18 | bm.allocN32Pixels(DIMENSION, DIMENSION); |
| 19 | SkASSERT(DIMENSION*4 == bm.rowBytes()); // ensure no padding on each row |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 20 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 21 | |
| 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.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 31 | SkPMColor andValue = ~0U; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 32 | 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.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 40 | bool success = shouldDraw ? (~0U == andValue) : (0 == orValue); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 41 | |
| 42 | if (!success) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 43 | const char* str; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 44 | if (shouldDraw) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 45 | str = "Path expected to draw everywhere, but didn't. "; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 46 | } else { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 47 | str = "Path expected to draw nowhere, but did. "; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 48 | } |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 49 | 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.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 53 | // uncomment this if you want to step in to see the failure |
| 54 | // canvas.drawPath(path, p); |
| 55 | } |
| 56 | } |
| 57 | |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 58 | enum DrawCaps { |
| 59 | kDontDrawCaps, |
| 60 | kDrawCaps |
| 61 | }; |
| 62 | |
| 63 | static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw, |
| 64 | DrawCaps drawCaps) { |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 65 | static const SkPaint::Cap gCaps[] = { |
| 66 | SkPaint::kButt_Cap, |
| 67 | SkPaint::kRound_Cap, |
| 68 | SkPaint::kSquare_Cap |
| 69 | }; |
| 70 | static const SkPaint::Join gJoins[] = { |
| 71 | SkPaint::kMiter_Join, |
| 72 | SkPaint::kRound_Join, |
| 73 | SkPaint::kBevel_Join |
| 74 | }; |
| 75 | static const SkPaint::Style gStyles[] = { |
| 76 | SkPaint::kFill_Style, |
| 77 | SkPaint::kStroke_Style, |
| 78 | SkPaint::kStrokeAndFill_Style |
| 79 | }; |
| 80 | for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) { |
| 81 | for (size_t join = 0; join < SK_ARRAY_COUNT(gJoins); ++join) { |
| 82 | for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) { |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 83 | if (drawCaps && SkPaint::kButt_Cap != gCaps[cap] |
| 84 | && SkPaint::kFill_Style != gStyles[style]) { |
| 85 | continue; |
| 86 | } |
| 87 | |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 88 | SkPaint paint; |
| 89 | paint.setStrokeWidth(SkIntToScalar(10)); |
| 90 | |
| 91 | paint.setStrokeCap(gCaps[cap]); |
| 92 | paint.setStrokeJoin(gJoins[join]); |
| 93 | paint.setStyle(gStyles[style]); |
| 94 | |
| 95 | paint.setAntiAlias(false); |
| 96 | drawAndTest(reporter, path, paint, shouldDraw); |
| 97 | paint.setAntiAlias(true); |
| 98 | drawAndTest(reporter, path, paint, shouldDraw); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #define CX (SkIntToScalar(DIMENSION) / 2) |
| 105 | #define CY (SkIntToScalar(DIMENSION) / 2) |
| 106 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 107 | static void make_empty(SkPath*) {} |
reed@google.com | 21e0bc2 | 2011-12-21 20:26:01 +0000 | [diff] [blame] | 108 | static void make_M(SkPath* path) { path->moveTo(CX, CY); } |
| 109 | static void make_MM(SkPath* path) { path->moveTo(CX, CY); path->moveTo(CX, CY); } |
| 110 | static void make_MZM(SkPath* path) { path->moveTo(CX, CY); path->close(); path->moveTo(CX, CY); } |
| 111 | static void make_L(SkPath* path) { path->moveTo(CX, CY); path->lineTo(CX, CY); } |
| 112 | static void make_Q(SkPath* path) { path->moveTo(CX, CY); path->quadTo(CX, CY, CX, CY); } |
| 113 | static void make_C(SkPath* path) { path->moveTo(CX, CY); path->cubicTo(CX, CY, CX, CY, CX, CY); } |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 114 | |
| 115 | /* Two invariants are tested: How does an empty/degenerate path draw? |
| 116 | * - if the path is drawn inverse, it should draw everywhere |
| 117 | * - if the path is drawn non-inverse, it should draw nowhere |
| 118 | * |
| 119 | * Things to iterate on: |
| 120 | * - path (empty, degenerate line/quad/cubic w/ and w/o close |
| 121 | * - paint style |
| 122 | * - path filltype |
| 123 | * - path stroke variants (e.g. caps, joins, width) |
| 124 | */ |
| 125 | static void test_emptydrawing(skiatest::Reporter* reporter) { |
| 126 | static void (*gMakeProc[])(SkPath*) = { |
reed@google.com | 21e0bc2 | 2011-12-21 20:26:01 +0000 | [diff] [blame] | 127 | make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 128 | }; |
| 129 | static SkPath::FillType gFills[] = { |
| 130 | SkPath::kWinding_FillType, |
| 131 | SkPath::kEvenOdd_FillType, |
| 132 | SkPath::kInverseWinding_FillType, |
| 133 | SkPath::kInverseEvenOdd_FillType |
| 134 | }; |
| 135 | for (int doClose = 0; doClose < 2; ++doClose) { |
| 136 | for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProc); ++i) { |
| 137 | SkPath path; |
| 138 | gMakeProc[i](&path); |
| 139 | if (doClose) { |
| 140 | path.close(); |
| 141 | } |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 142 | /* zero length segments and close following moves draw round and square caps */ |
| 143 | bool allowCaps = make_L == gMakeProc[i] || make_Q == gMakeProc[i] |
| 144 | || make_C == gMakeProc[i] || make_MZM == gMakeProc[i]; |
| 145 | allowCaps |= SkToBool(doClose); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 146 | for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) { |
| 147 | path.setFillType(gFills[fill]); |
| 148 | bool shouldDraw = path.isInverseFillType(); |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 149 | iter_paint(reporter, path, shouldDraw, allowCaps ? kDrawCaps : kDontDrawCaps); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 155 | DEF_TEST(EmptyPath, reporter) { |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 156 | test_emptydrawing(reporter); |
| 157 | } |