mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 8 | #include "gm.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 9 | #include "SkPath.h" |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 10 | #include "SkRandom.h" |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 11 | #include "SkDashPathEffect.h" |
| 12 | #include "SkParsePath.h" |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 13 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 14 | #define W 400 |
| 15 | #define H 400 |
| 16 | #define N 50 |
| 17 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 18 | constexpr SkScalar SW = SkIntToScalar(W); |
| 19 | constexpr SkScalar SH = SkIntToScalar(H); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 20 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 21 | static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 22 | SkScalar x = rand.nextUScalar1() * W; |
| 23 | SkScalar y = rand.nextUScalar1() * H; |
| 24 | SkScalar w = rand.nextUScalar1() * (W >> 2); |
| 25 | SkScalar h = rand.nextUScalar1() * (H >> 2); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 26 | SkScalar hoffset = rand.nextSScalar1(); |
| 27 | SkScalar woffset = rand.nextSScalar1(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 28 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 29 | r->set(x, y, x + w, y + h); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 30 | r->offset(-w/2 + woffset, -h/2 + hoffset); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 32 | paint->setColor(rand.nextU()); |
| 33 | paint->setAlpha(0xFF); |
| 34 | } |
| 35 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 37 | class StrokesGM : public skiagm::GM { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 38 | public: |
| 39 | StrokesGM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 41 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkString onShortName() override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 44 | return SkString("strokes_round"); |
| 45 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | SkISize onISize() override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 48 | return SkISize::Make(W, H*2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 49 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 50 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 51 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 52 | SkPaint paint; |
| 53 | paint.setStyle(SkPaint::kStroke_Style); |
| 54 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 56 | for (int y = 0; y < 2; y++) { |
| 57 | paint.setAntiAlias(!!y); |
| 58 | SkAutoCanvasRestore acr(canvas, true); |
| 59 | canvas->translate(0, SH * y); |
| 60 | canvas->clipRect(SkRect::MakeLTRB( |
| 61 | SkIntToScalar(2), SkIntToScalar(2) |
| 62 | , SW - SkIntToScalar(2), SH - SkIntToScalar(2) |
| 63 | )); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 65 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 66 | for (int i = 0; i < N; i++) { |
| 67 | SkRect r; |
| 68 | rnd_rect(&r, &paint, rand); |
| 69 | canvas->drawOval(r, paint); |
| 70 | rnd_rect(&r, &paint, rand); |
| 71 | canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint); |
| 72 | rnd_rect(&r, &paint, rand); |
| 73 | } |
| 74 | } |
| 75 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 77 | private: |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 78 | typedef skiagm::GM INHERITED; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 81 | /* See |
| 82 | https://code.google.com/p/chromium/issues/detail?id=422974 and |
| 83 | http://jsfiddle.net/1xnku3sg/2/ |
| 84 | */ |
| 85 | class ZeroLenStrokesGM : public skiagm::GM { |
| 86 | SkPath fMoveHfPath, fMoveZfPath, fDashedfPath, fRefPath[4]; |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 87 | SkPath fCubicPath, fQuadPath, fLinePath; |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 88 | protected: |
| 89 | void onOnceBeforeDraw() override { |
| 90 | |
| 91 | SkAssertResult(SkParsePath::FromSVGString("M0,0h0M10,0h0M20,0h0", &fMoveHfPath)); |
| 92 | SkAssertResult(SkParsePath::FromSVGString("M0,0zM10,0zM20,0z", &fMoveZfPath)); |
| 93 | SkAssertResult(SkParsePath::FromSVGString("M0,0h25", &fDashedfPath)); |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 94 | SkAssertResult(SkParsePath::FromSVGString("M 0 0 C 0 0 0 0 0 0", &fCubicPath)); |
| 95 | SkAssertResult(SkParsePath::FromSVGString("M 0 0 Q 0 0 0 0", &fQuadPath)); |
| 96 | SkAssertResult(SkParsePath::FromSVGString("M 0 0 L 0 0", &fLinePath)); |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 97 | |
| 98 | for (int i = 0; i < 3; ++i) { |
| 99 | fRefPath[0].addCircle(i * 10.f, 0, 5); |
| 100 | fRefPath[1].addCircle(i * 10.f, 0, 10); |
| 101 | fRefPath[2].addRect(i * 10.f - 4, -2, i * 10.f + 4, 6); |
| 102 | fRefPath[3].addRect(i * 10.f - 10, -10, i * 10.f + 10, 10); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | SkString onShortName() override { |
| 107 | return SkString("zeroPath"); |
| 108 | } |
| 109 | |
| 110 | SkISize onISize() override { |
| 111 | return SkISize::Make(W, H*2); |
| 112 | } |
| 113 | |
| 114 | void onDraw(SkCanvas* canvas) override { |
| 115 | SkPaint fillPaint, strokePaint, dashPaint; |
| 116 | fillPaint.setAntiAlias(true); |
| 117 | strokePaint = fillPaint; |
| 118 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 119 | for (int i = 0; i < 2; ++i) { |
| 120 | fillPaint.setAlpha(255); |
| 121 | strokePaint.setAlpha(255); |
| 122 | strokePaint.setStrokeWidth(i ? 8.f : 10.f); |
| 123 | strokePaint.setStrokeCap(i ? SkPaint::kSquare_Cap : SkPaint::kRound_Cap); |
| 124 | canvas->save(); |
| 125 | canvas->translate(10 + i * 100.f, 10); |
| 126 | canvas->drawPath(fMoveHfPath, strokePaint); |
| 127 | canvas->translate(0, 20); |
| 128 | canvas->drawPath(fMoveZfPath, strokePaint); |
| 129 | dashPaint = strokePaint; |
| 130 | const SkScalar intervals[] = { 0, 10 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 131 | dashPaint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0)); |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 132 | SkPath fillPath; |
| 133 | dashPaint.getFillPath(fDashedfPath, &fillPath); |
| 134 | canvas->translate(0, 20); |
| 135 | canvas->drawPath(fDashedfPath, dashPaint); |
| 136 | canvas->translate(0, 20); |
| 137 | canvas->drawPath(fRefPath[i * 2], fillPaint); |
| 138 | strokePaint.setStrokeWidth(20); |
| 139 | strokePaint.setAlpha(127); |
| 140 | canvas->translate(0, 50); |
| 141 | canvas->drawPath(fMoveHfPath, strokePaint); |
| 142 | canvas->translate(0, 30); |
| 143 | canvas->drawPath(fMoveZfPath, strokePaint); |
| 144 | canvas->translate(0, 30); |
| 145 | fillPaint.setAlpha(127); |
| 146 | canvas->drawPath(fRefPath[1 + i * 2], fillPaint); |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 147 | canvas->translate(0, 30); |
| 148 | canvas->drawPath(fCubicPath, strokePaint); |
| 149 | canvas->translate(0, 30); |
| 150 | canvas->drawPath(fQuadPath, strokePaint); |
| 151 | canvas->translate(0, 30); |
| 152 | canvas->drawPath(fLinePath, strokePaint); |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 153 | canvas->restore(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private: |
| 158 | typedef skiagm::GM INHERITED; |
| 159 | }; |
| 160 | |
caryclark | 950305e | 2015-10-26 08:17:04 -0700 | [diff] [blame] | 161 | class TeenyStrokesGM : public skiagm::GM { |
| 162 | |
| 163 | SkString onShortName() override { |
| 164 | return SkString("teenyStrokes"); |
| 165 | } |
| 166 | |
| 167 | SkISize onISize() override { |
| 168 | return SkISize::Make(W, H*2); |
| 169 | } |
| 170 | |
| 171 | static void line(SkScalar scale, SkCanvas* canvas, SkColor color) { |
| 172 | SkPaint p; |
| 173 | p.setAntiAlias(true); |
| 174 | p.setStyle(SkPaint::kStroke_Style); |
| 175 | p.setColor(color); |
| 176 | canvas->translate(50, 0); |
| 177 | canvas->save(); |
| 178 | p.setStrokeWidth(scale * 5); |
| 179 | canvas->scale(1 / scale, 1 / scale); |
| 180 | canvas->drawLine(20 * scale, 20 * scale, 20 * scale, 100 * scale, p); |
| 181 | canvas->drawLine(20 * scale, 20 * scale, 100 * scale, 100 * scale, p); |
| 182 | canvas->restore(); |
| 183 | } |
| 184 | |
| 185 | void onDraw(SkCanvas* canvas) override { |
| 186 | line(0.00005f, canvas, SK_ColorBLACK); |
| 187 | line(0.000045f, canvas, SK_ColorRED); |
| 188 | line(0.0000035f, canvas, SK_ColorGREEN); |
| 189 | line(0.000003f, canvas, SK_ColorBLUE); |
| 190 | line(0.000002f, canvas, SK_ColorBLACK); |
| 191 | } |
| 192 | private: |
| 193 | typedef skiagm::GM INHERITED; |
| 194 | }; |
| 195 | |
caryclark | b775e91 | 2015-11-30 13:47:11 -0800 | [diff] [blame] | 196 | DEF_SIMPLE_GM(CubicStroke, canvas, 384, 384) { |
| 197 | SkPaint p; |
| 198 | p.setAntiAlias(true); |
| 199 | p.setStyle(SkPaint::kStroke_Style); |
| 200 | p.setStrokeWidth(1.0720f); |
| 201 | SkPath path; |
| 202 | path.moveTo(-6000,-6000); |
| 203 | path.cubicTo(-3500,5500,-500,5500,2500,-6500); |
| 204 | canvas->drawPath(path, p); |
| 205 | p.setStrokeWidth(1.0721f); |
| 206 | canvas->translate(10, 10); |
| 207 | canvas->drawPath(path, p); |
| 208 | p.setStrokeWidth(1.0722f); |
| 209 | canvas->translate(10, 10); |
| 210 | canvas->drawPath(path, p); |
| 211 | } |
caryclark | 950305e | 2015-10-26 08:17:04 -0700 | [diff] [blame] | 212 | |
caryclark | 57eecc1 | 2015-12-08 04:29:45 -0800 | [diff] [blame] | 213 | DEF_SIMPLE_GM(zerolinestroke, canvas, 90, 120) { |
| 214 | SkPaint paint; |
| 215 | paint.setStyle(SkPaint::kStroke_Style); |
| 216 | paint.setStrokeWidth(20); |
| 217 | paint.setAntiAlias(true); |
| 218 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 219 | |
| 220 | SkPath path; |
| 221 | path.moveTo(30, 90); |
| 222 | path.lineTo(30, 90); |
| 223 | path.lineTo(60, 90); |
| 224 | path.lineTo(60, 90); |
| 225 | canvas->drawPath(path, paint); |
| 226 | |
| 227 | path.reset(); |
| 228 | path.moveTo(30, 30); |
| 229 | path.lineTo(60, 30); |
| 230 | canvas->drawPath(path, paint); |
| 231 | |
| 232 | path.reset(); |
| 233 | path.moveTo(30, 60); |
| 234 | path.lineTo(30, 60); |
| 235 | path.lineTo(60, 60); |
| 236 | canvas->drawPath(path, paint); |
| 237 | } |
| 238 | |
caryclark | 40b7d3b | 2015-12-22 06:13:33 -0800 | [diff] [blame] | 239 | DEF_SIMPLE_GM(quadcap, canvas, 200, 200) { |
| 240 | SkPaint p; |
| 241 | p.setAntiAlias(true); |
| 242 | p.setStyle(SkPaint::kStroke_Style); |
| 243 | p.setStrokeWidth(0); |
| 244 | SkPath path; |
| 245 | SkPoint pts[] = {{105.738571f,13.126318f}, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 246 | {105.738571f,13.126318f}, |
caryclark | 40b7d3b | 2015-12-22 06:13:33 -0800 | [diff] [blame] | 247 | {123.753784f,1.f}}; |
| 248 | SkVector tangent = pts[1] - pts[2]; |
| 249 | tangent.normalize(); |
| 250 | SkPoint pts2[3]; |
| 251 | memcpy(pts2, pts, sizeof(pts)); |
| 252 | const SkScalar capOutset = SK_ScalarPI / 8; |
| 253 | pts2[0].fX += tangent.fX * capOutset; |
| 254 | pts2[0].fY += tangent.fY * capOutset; |
| 255 | pts2[1].fX += tangent.fX * capOutset; |
| 256 | pts2[1].fY += tangent.fY * capOutset; |
| 257 | pts2[2].fX += -tangent.fX * capOutset; |
| 258 | pts2[2].fY += -tangent.fY * capOutset; |
| 259 | path.moveTo(pts2[0]); |
| 260 | path.quadTo(pts2[1], pts2[2]); |
| 261 | canvas->drawPath(path, p); |
| 262 | |
| 263 | path.reset(); |
| 264 | path.moveTo(pts[0]); |
| 265 | path.quadTo(pts[1], pts[2]); |
| 266 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 267 | canvas->translate(30, 0); |
| 268 | canvas->drawPath(path, p); |
| 269 | } |
| 270 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 271 | class Strokes2GM : public skiagm::GM { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 272 | SkPath fPath; |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 273 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 274 | void onOnceBeforeDraw() override { |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 275 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 276 | fPath.moveTo(0, 0); |
| 277 | for (int i = 0; i < 13; i++) { |
| 278 | SkScalar x = rand.nextUScalar1() * (W >> 1); |
| 279 | SkScalar y = rand.nextUScalar1() * (H >> 1); |
| 280 | fPath.lineTo(x, y); |
| 281 | } |
| 282 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 283 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 284 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 285 | SkString onShortName() override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 286 | return SkString("strokes_poly"); |
| 287 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 288 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 289 | SkISize onISize() override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 290 | return SkISize::Make(W, H*2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 291 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 292 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 293 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 294 | canvas->drawColor(SK_ColorWHITE); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 295 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 296 | SkPaint paint; |
| 297 | paint.setStyle(SkPaint::kStroke_Style); |
| 298 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 299 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 300 | for (int y = 0; y < 2; y++) { |
| 301 | paint.setAntiAlias(!!y); |
| 302 | SkAutoCanvasRestore acr(canvas, true); |
| 303 | canvas->translate(0, SH * y); |
| 304 | canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2), |
| 305 | SkIntToScalar(2), |
| 306 | SW - SkIntToScalar(2), |
| 307 | SH - SkIntToScalar(2))); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 308 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 309 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 310 | for (int i = 0; i < N/2; i++) { |
| 311 | SkRect r; |
| 312 | rnd_rect(&r, &paint, rand); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 313 | canvas->rotate(SkIntToScalar(15), SW/2, SH/2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 314 | canvas->drawPath(fPath, paint); |
| 315 | } |
| 316 | } |
| 317 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 318 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 319 | private: |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 320 | typedef skiagm::GM INHERITED; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | ////////////////////////////////////////////////////////////////////////////// |
| 324 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 325 | static SkRect inset(const SkRect& r) { |
| 326 | SkRect rr(r); |
| 327 | rr.inset(r.width()/10, r.height()/10); |
| 328 | return rr; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 329 | } |
| 330 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 331 | class Strokes3GM : public skiagm::GM { |
| 332 | static void make0(SkPath* path, const SkRect& bounds, SkString* title) { |
| 333 | path->addRect(bounds, SkPath::kCW_Direction); |
| 334 | path->addRect(inset(bounds), SkPath::kCW_Direction); |
| 335 | title->set("CW CW"); |
| 336 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 337 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 338 | static void make1(SkPath* path, const SkRect& bounds, SkString* title) { |
| 339 | path->addRect(bounds, SkPath::kCW_Direction); |
| 340 | path->addRect(inset(bounds), SkPath::kCCW_Direction); |
| 341 | title->set("CW CCW"); |
| 342 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 343 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 344 | static void make2(SkPath* path, const SkRect& bounds, SkString* title) { |
| 345 | path->addOval(bounds, SkPath::kCW_Direction); |
| 346 | path->addOval(inset(bounds), SkPath::kCW_Direction); |
| 347 | title->set("CW CW"); |
| 348 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 349 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 350 | static void make3(SkPath* path, const SkRect& bounds, SkString* title) { |
| 351 | path->addOval(bounds, SkPath::kCW_Direction); |
| 352 | path->addOval(inset(bounds), SkPath::kCCW_Direction); |
| 353 | title->set("CW CCW"); |
| 354 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 355 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 356 | static void make4(SkPath* path, const SkRect& bounds, SkString* title) { |
| 357 | path->addRect(bounds, SkPath::kCW_Direction); |
| 358 | SkRect r = bounds; |
| 359 | r.inset(bounds.width() / 10, -bounds.height() / 10); |
| 360 | path->addOval(r, SkPath::kCW_Direction); |
| 361 | title->set("CW CW"); |
| 362 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 363 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 364 | static void make5(SkPath* path, const SkRect& bounds, SkString* title) { |
| 365 | path->addRect(bounds, SkPath::kCW_Direction); |
| 366 | SkRect r = bounds; |
| 367 | r.inset(bounds.width() / 10, -bounds.height() / 10); |
| 368 | path->addOval(r, SkPath::kCCW_Direction); |
| 369 | title->set("CW CCW"); |
| 370 | } |
| 371 | |
| 372 | public: |
| 373 | Strokes3GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 374 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 375 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 376 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 377 | SkString onShortName() override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 378 | return SkString("strokes3"); |
| 379 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 380 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 381 | SkISize onISize() override { |
caryclark | 3760457 | 2015-02-23 06:51:04 -0800 | [diff] [blame] | 382 | return SkISize::Make(1500, 1500); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 383 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 384 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 385 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 386 | SkPaint origPaint; |
| 387 | origPaint.setAntiAlias(true); |
| 388 | origPaint.setStyle(SkPaint::kStroke_Style); |
| 389 | SkPaint fillPaint(origPaint); |
| 390 | fillPaint.setColor(SK_ColorRED); |
| 391 | SkPaint strokePaint(origPaint); |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 392 | strokePaint.setColor(sk_tool_utils::color_to_565(0xFF4444FF)); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 393 | |
| 394 | void (*procs[])(SkPath*, const SkRect&, SkString*) = { |
| 395 | make0, make1, make2, make3, make4, make5 |
| 396 | }; |
| 397 | |
caryclark | 3760457 | 2015-02-23 06:51:04 -0800 | [diff] [blame] | 398 | canvas->translate(SkIntToScalar(20), SkIntToScalar(80)); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 399 | |
| 400 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50)); |
| 401 | SkScalar dx = bounds.width() * 4/3; |
| 402 | SkScalar dy = bounds.height() * 5; |
| 403 | |
| 404 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
| 405 | SkPath orig; |
| 406 | SkString str; |
| 407 | procs[i](&orig, bounds, &str); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 408 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 409 | canvas->save(); |
| 410 | for (int j = 0; j < 13; ++j) { |
| 411 | strokePaint.setStrokeWidth(SK_Scalar1 * j * j); |
| 412 | canvas->drawPath(orig, strokePaint); |
| 413 | canvas->drawPath(orig, origPaint); |
| 414 | SkPath fill; |
| 415 | strokePaint.getFillPath(orig, &fill); |
| 416 | canvas->drawPath(fill, fillPaint); |
| 417 | canvas->translate(dx + strokePaint.getStrokeWidth(), 0); |
| 418 | } |
| 419 | canvas->restore(); |
| 420 | canvas->translate(0, dy); |
| 421 | } |
| 422 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 423 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 424 | private: |
| 425 | typedef skiagm::GM INHERITED; |
| 426 | }; |
| 427 | |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 428 | class Strokes4GM : public skiagm::GM { |
| 429 | public: |
| 430 | Strokes4GM() {} |
| 431 | |
| 432 | protected: |
| 433 | |
| 434 | SkString onShortName() override { |
| 435 | return SkString("strokes_zoomed"); |
| 436 | } |
| 437 | |
| 438 | SkISize onISize() override { |
| 439 | return SkISize::Make(W, H*2); |
| 440 | } |
| 441 | |
| 442 | void onDraw(SkCanvas* canvas) override { |
| 443 | SkPaint paint; |
| 444 | paint.setStyle(SkPaint::kStroke_Style); |
| 445 | paint.setStrokeWidth(0.055f); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 446 | |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 447 | canvas->scale(1000, 1000); |
| 448 | canvas->drawCircle(0, 2, 1.97f, paint); |
| 449 | } |
| 450 | |
| 451 | private: |
| 452 | typedef skiagm::GM INHERITED; |
| 453 | }; |
| 454 | |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 455 | // Test stroking for curves that produce degenerate tangents when t is 0 or 1 (see bug 4191) |
| 456 | class Strokes5GM : public skiagm::GM { |
| 457 | public: |
| 458 | Strokes5GM() {} |
| 459 | |
| 460 | protected: |
| 461 | |
| 462 | SkString onShortName() override { |
| 463 | return SkString("zero_control_stroke"); |
| 464 | } |
| 465 | |
| 466 | SkISize onISize() override { |
| 467 | return SkISize::Make(W, H*2); |
| 468 | } |
| 469 | |
| 470 | void onDraw(SkCanvas* canvas) override { |
| 471 | SkPaint p; |
| 472 | p.setColor(SK_ColorRED); |
| 473 | p.setAntiAlias(true); |
| 474 | p.setStyle(SkPaint::kStroke_Style); |
| 475 | p.setStrokeWidth(40); |
| 476 | p.setStrokeCap(SkPaint::kButt_Cap); |
| 477 | |
| 478 | SkPath path; |
| 479 | path.moveTo(157.474f,111.753f); |
| 480 | path.cubicTo(128.5f,111.5f,35.5f,29.5f,35.5f,29.5f); |
| 481 | canvas->drawPath(path, p); |
| 482 | path.reset(); |
| 483 | path.moveTo(250, 50); |
| 484 | path.quadTo(280, 80, 280, 80); |
| 485 | canvas->drawPath(path, p); |
| 486 | path.reset(); |
| 487 | path.moveTo(150, 50); |
| 488 | path.conicTo(180, 80, 180, 80, 0.707f); |
| 489 | canvas->drawPath(path, p); |
| 490 | |
| 491 | path.reset(); |
| 492 | path.moveTo(157.474f,311.753f); |
| 493 | path.cubicTo(157.474f,311.753f,85.5f,229.5f,35.5f,229.5f); |
| 494 | canvas->drawPath(path, p); |
| 495 | path.reset(); |
| 496 | path.moveTo(280, 250); |
| 497 | path.quadTo(280, 250, 310, 280); |
| 498 | canvas->drawPath(path, p); |
| 499 | path.reset(); |
| 500 | path.moveTo(180, 250); |
| 501 | path.conicTo(180, 250, 210, 280, 0.707f); |
| 502 | canvas->drawPath(path, p); |
| 503 | } |
| 504 | |
| 505 | private: |
| 506 | typedef skiagm::GM INHERITED; |
| 507 | }; |
| 508 | |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 509 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 510 | ////////////////////////////////////////////////////////////////////////////// |
| 511 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 512 | DEF_GM( return new StrokesGM; ) |
| 513 | DEF_GM( return new Strokes2GM; ) |
| 514 | DEF_GM( return new Strokes3GM; ) |
| 515 | DEF_GM( return new Strokes4GM; ) |
| 516 | DEF_GM( return new Strokes5GM; ) |
caryclark | 5cb00a9 | 2015-08-26 09:04:55 -0700 | [diff] [blame] | 517 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 518 | DEF_GM( return new ZeroLenStrokesGM; ) |
caryclark | 950305e | 2015-10-26 08:17:04 -0700 | [diff] [blame] | 519 | DEF_GM( return new TeenyStrokesGM; ) |