epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 8 | #include "gm.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 9 | #include "SkPath.h" |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 10 | |
| 11 | typedef SkScalar (*MakePathProc)(SkPath*); |
| 12 | |
| 13 | static SkScalar make_frame(SkPath* path) { |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 14 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 15 | SkIntToScalar(630), SkIntToScalar(470) }; |
| 16 | path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 18 | SkPaint paint; |
| 19 | paint.setStyle(SkPaint::kStroke_Style); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 20 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 21 | paint.getFillPath(*path, path); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 22 | return SkIntToScalar(15); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static SkScalar make_triangle(SkPath* path) { |
| 26 | static const int gCoord[] = { |
| 27 | 10, 20, 15, 5, 30, 30 |
| 28 | }; |
| 29 | path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1])); |
| 30 | path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3])); |
| 31 | path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5])); |
| 32 | path->close(); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 33 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 34 | return SkIntToScalar(30); |
| 35 | } |
| 36 | |
| 37 | static SkScalar make_rect(SkPath* path) { |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 38 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 39 | SkIntToScalar(30), SkIntToScalar(30) }; |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 40 | path->addRect(r); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 41 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 42 | return SkIntToScalar(30); |
| 43 | } |
| 44 | |
| 45 | static SkScalar make_oval(SkPath* path) { |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 46 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 47 | SkIntToScalar(30), SkIntToScalar(30) }; |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 48 | path->addOval(r); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 49 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 50 | return SkIntToScalar(30); |
| 51 | } |
| 52 | |
| 53 | static SkScalar make_sawtooth(SkPath* path) { |
| 54 | SkScalar x = SkIntToScalar(20); |
| 55 | SkScalar y = SkIntToScalar(20); |
| 56 | const SkScalar x0 = x; |
| 57 | const SkScalar dx = SK_Scalar1 * 5; |
| 58 | const SkScalar dy = SK_Scalar1 * 10; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 59 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 60 | path->moveTo(x, y); |
| 61 | for (int i = 0; i < 32; i++) { |
| 62 | x += dx; |
| 63 | path->lineTo(x, y - dy); |
| 64 | x += dx; |
| 65 | path->lineTo(x, y + dy); |
| 66 | } |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 67 | path->lineTo(x, y + (2 * dy)); |
| 68 | path->lineTo(x0, y + (2 * dy)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 69 | path->close(); |
| 70 | return SkIntToScalar(30); |
| 71 | } |
| 72 | |
| 73 | static SkScalar make_star(SkPath* path, int n) { |
| 74 | const SkScalar c = SkIntToScalar(45); |
| 75 | const SkScalar r = SkIntToScalar(20); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 77 | SkScalar rad = -SK_ScalarPI / 2; |
| 78 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 80 | path->moveTo(c, c - r); |
| 81 | for (int i = 1; i < n; i++) { |
| 82 | rad += drad; |
| 83 | SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV); |
| 84 | path->lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r)); |
| 85 | } |
| 86 | path->close(); |
| 87 | return r * 2 * 6 / 5; |
| 88 | } |
| 89 | |
| 90 | static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); } |
| 91 | static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); } |
| 92 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 93 | // We don't expect any output from this path. |
| 94 | static SkScalar make_line(SkPath* path) { |
| 95 | path->moveTo(SkIntToScalar(30), SkIntToScalar(30)); |
| 96 | path->lineTo(SkIntToScalar(120), SkIntToScalar(40)); |
| 97 | path->close(); |
| 98 | path->moveTo(SkIntToScalar(150), SkIntToScalar(30)); |
| 99 | path->lineTo(SkIntToScalar(150), SkIntToScalar(30)); |
| 100 | path->lineTo(SkIntToScalar(300), SkIntToScalar(40)); |
| 101 | path->close(); |
| 102 | return SkIntToScalar(40); |
| 103 | } |
| 104 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 105 | static const MakePathProc gProcs[] = { |
| 106 | make_frame, |
| 107 | make_triangle, |
| 108 | make_rect, |
| 109 | make_oval, |
| 110 | make_sawtooth, |
| 111 | make_star_5, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 112 | make_star_13, |
| 113 | make_line, |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | #define N SK_ARRAY_COUNT(gProcs) |
| 117 | |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 118 | class PathFillGM : public skiagm::GM { |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 119 | SkPath fPath[N]; |
| 120 | SkScalar fDY[N]; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 121 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 122 | void onOnceBeforeDraw() override { |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 123 | for (size_t i = 0; i < N; i++) { |
| 124 | fDY[i] = gProcs[i](&fPath[i]); |
| 125 | } |
| 126 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 128 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 129 | SkString onShortName() override { |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 130 | return SkString("pathfill"); |
| 131 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 133 | SkISize onISize() override { |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 134 | return SkISize::Make(640, 480); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 135 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 136 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 137 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 138 | SkPaint paint; |
| 139 | paint.setAntiAlias(true); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 140 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 141 | for (size_t i = 0; i < N; i++) { |
| 142 | canvas->drawPath(fPath[i], paint); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 143 | canvas->translate(SkIntToScalar(0), fDY[i]); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 146 | |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 147 | private: |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 148 | typedef skiagm::GM INHERITED; |
| 149 | }; |
| 150 | |
| 151 | // test inverse-fill w/ a clip that completely excludes the geometry |
| 152 | class PathInverseFillGM : public skiagm::GM { |
| 153 | SkPath fPath[N]; |
| 154 | SkScalar fDY[N]; |
caryclark | 88c748a | 2015-02-18 10:56:00 -0800 | [diff] [blame] | 155 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 156 | void onOnceBeforeDraw() override { |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 157 | for (size_t i = 0; i < N; i++) { |
| 158 | fDY[i] = gProcs[i](&fPath[i]); |
| 159 | } |
| 160 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 161 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 162 | SkString onShortName() override { |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 163 | return SkString("pathinvfill"); |
| 164 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 165 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 166 | SkISize onISize() override { |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 167 | return SkISize::Make(450, 220); |
| 168 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 169 | |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 170 | static void show(SkCanvas* canvas, const SkPath& path, const SkPaint& paint, |
| 171 | const SkRect* clip, SkScalar top, const SkScalar bottom) { |
| 172 | canvas->save(); |
| 173 | if (clip) { |
| 174 | SkRect r = *clip; |
| 175 | r.fTop = top; |
| 176 | r.fBottom = bottom; |
| 177 | canvas->clipRect(r); |
| 178 | } |
| 179 | canvas->drawPath(path, paint); |
| 180 | canvas->restore(); |
| 181 | } |
| 182 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 183 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 184 | SkPath path; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 186 | path.addCircle(SkIntToScalar(50), SkIntToScalar(50), SkIntToScalar(40)); |
| 187 | path.toggleInverseFillType(); |
| 188 | |
| 189 | SkRect clipR = { 0, 0, SkIntToScalar(100), SkIntToScalar(200) }; |
| 190 | |
| 191 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 192 | |
| 193 | for (int doclip = 0; doclip <= 1; ++doclip) { |
| 194 | for (int aa = 0; aa <= 1; ++aa) { |
| 195 | SkPaint paint; |
| 196 | paint.setAntiAlias(SkToBool(aa)); |
| 197 | |
| 198 | canvas->save(); |
| 199 | canvas->clipRect(clipR); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 200 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | const SkRect* clipPtr = doclip ? &clipR : nullptr; |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 202 | |
| 203 | show(canvas, path, paint, clipPtr, clipR.fTop, clipR.centerY()); |
| 204 | show(canvas, path, paint, clipPtr, clipR.centerY(), clipR.fBottom); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 205 | |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 206 | canvas->restore(); |
| 207 | canvas->translate(SkIntToScalar(110), 0); |
| 208 | } |
| 209 | } |
| 210 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 211 | |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 212 | private: |
| 213 | typedef skiagm::GM INHERITED; |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | /////////////////////////////////////////////////////////////////////////////// |
| 217 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 218 | DEF_GM( return new PathFillGM; ) |
| 219 | DEF_GM( return new PathInverseFillGM; ) |