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 | |
| 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 10 | #include "SkPath.h" |
| 11 | #include "SkCanvas.h" |
| 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; |
| 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.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 21 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 22 | |
| 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.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 32 | SkPMColor andValue = ~0U; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 33 | 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.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 41 | bool success = shouldDraw ? (~0U == andValue) : (0 == orValue); |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 42 | |
| 43 | if (!success) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame^] | 44 | const char* str; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 45 | if (shouldDraw) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame^] | 46 | str = "Path expected to draw everywhere, but didn't. "; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 47 | } else { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame^] | 48 | str = "Path expected to draw nowhere, but did. "; |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 49 | } |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame^] | 50 | 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.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 54 | // uncomment this if you want to step in to see the failure |
| 55 | // canvas.drawPath(path, p); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static 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.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 97 | static void make_empty(SkPath*) {} |
reed@google.com | 21e0bc2 | 2011-12-21 20:26:01 +0000 | [diff] [blame] | 98 | static void make_M(SkPath* path) { path->moveTo(CX, CY); } |
| 99 | static void make_MM(SkPath* path) { path->moveTo(CX, CY); path->moveTo(CX, CY); } |
| 100 | static void make_MZM(SkPath* path) { path->moveTo(CX, CY); path->close(); path->moveTo(CX, CY); } |
| 101 | static void make_L(SkPath* path) { path->moveTo(CX, CY); path->lineTo(CX, CY); } |
| 102 | static void make_Q(SkPath* path) { path->moveTo(CX, CY); path->quadTo(CX, CY, CX, CY); } |
| 103 | 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] | 104 | |
| 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 | */ |
| 115 | static void test_emptydrawing(skiatest::Reporter* reporter) { |
| 116 | static void (*gMakeProc[])(SkPath*) = { |
reed@google.com | 21e0bc2 | 2011-12-21 20:26:01 +0000 | [diff] [blame] | 117 | 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] | 118 | }; |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 141 | DEF_TEST(EmptyPath, reporter) { |
reed@google.com | ee068aa | 2011-12-21 19:36:21 +0000 | [diff] [blame] | 142 | test_emptydrawing(reporter); |
| 143 | } |