blob: 19ba84401d168a81ae8a454ba64d772b8ecaedec [file] [log] [blame]
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.orgf2c21cd2011-06-18 00:15:04 +00006 */
7
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +00008#include "gm.h"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkPath.h"
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000010#include "SkRandom.h"
caryclark5cb00a92015-08-26 09:04:55 -070011#include "SkDashPathEffect.h"
12#include "SkParsePath.h"
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000013
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000014#define W 400
15#define H 400
16#define N 50
17
18static const SkScalar SW = SkIntToScalar(W);
19static const SkScalar SH = SkIntToScalar(H);
20
scroggof9d61012014-12-15 12:54:51 -080021static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000022 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.com17b78942011-08-26 14:40:38 +000026 SkScalar hoffset = rand.nextSScalar1();
27 SkScalar woffset = rand.nextSScalar1();
rmistry@google.comae933ce2012-08-23 18:19:56 +000028
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000029 r->set(x, y, x + w, y + h);
epoger@google.com17b78942011-08-26 14:40:38 +000030 r->offset(-w/2 + woffset, -h/2 + hoffset);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000032 paint->setColor(rand.nextU());
33 paint->setAlpha(0xFF);
34}
35
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
reed@google.com4384fab2012-06-05 16:14:23 +000037class StrokesGM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000038public:
39 StrokesGM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000040
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000041protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000042
mtklein36352bf2015-03-25 18:17:31 -070043 SkString onShortName() override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000044 return SkString("strokes_round");
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
mtklein36352bf2015-03-25 18:17:31 -070047 SkISize onISize() override {
reed@google.com4384fab2012-06-05 16:14:23 +000048 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000049 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000050
mtklein36352bf2015-03-25 18:17:31 -070051 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000052 SkPaint paint;
53 paint.setStyle(SkPaint::kStroke_Style);
54 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000056 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.comae933ce2012-08-23 18:19:56 +000064
scroggof9d61012014-12-15 12:54:51 -080065 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000066 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.comae933ce2012-08-23 18:19:56 +000076
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000077private:
reed@google.com4384fab2012-06-05 16:14:23 +000078 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000079};
80
caryclark5cb00a92015-08-26 09:04:55 -070081/* See
82 https://code.google.com/p/chromium/issues/detail?id=422974 and
83 http://jsfiddle.net/1xnku3sg/2/
84 */
85class ZeroLenStrokesGM : public skiagm::GM {
86 SkPath fMoveHfPath, fMoveZfPath, fDashedfPath, fRefPath[4];
87protected:
88 void onOnceBeforeDraw() override {
89
90 SkAssertResult(SkParsePath::FromSVGString("M0,0h0M10,0h0M20,0h0", &fMoveHfPath));
91 SkAssertResult(SkParsePath::FromSVGString("M0,0zM10,0zM20,0z", &fMoveZfPath));
92 SkAssertResult(SkParsePath::FromSVGString("M0,0h25", &fDashedfPath));
93
94 for (int i = 0; i < 3; ++i) {
95 fRefPath[0].addCircle(i * 10.f, 0, 5);
96 fRefPath[1].addCircle(i * 10.f, 0, 10);
97 fRefPath[2].addRect(i * 10.f - 4, -2, i * 10.f + 4, 6);
98 fRefPath[3].addRect(i * 10.f - 10, -10, i * 10.f + 10, 10);
99 }
100 }
101
102 SkString onShortName() override {
103 return SkString("zeroPath");
104 }
105
106 SkISize onISize() override {
107 return SkISize::Make(W, H*2);
108 }
109
110 void onDraw(SkCanvas* canvas) override {
111 SkPaint fillPaint, strokePaint, dashPaint;
112 fillPaint.setAntiAlias(true);
113 strokePaint = fillPaint;
114 strokePaint.setStyle(SkPaint::kStroke_Style);
115 for (int i = 0; i < 2; ++i) {
116 fillPaint.setAlpha(255);
117 strokePaint.setAlpha(255);
118 strokePaint.setStrokeWidth(i ? 8.f : 10.f);
119 strokePaint.setStrokeCap(i ? SkPaint::kSquare_Cap : SkPaint::kRound_Cap);
120 canvas->save();
121 canvas->translate(10 + i * 100.f, 10);
122 canvas->drawPath(fMoveHfPath, strokePaint);
123 canvas->translate(0, 20);
124 canvas->drawPath(fMoveZfPath, strokePaint);
125 dashPaint = strokePaint;
126 const SkScalar intervals[] = { 0, 10 };
127 dashPaint.setPathEffect(SkDashPathEffect::Create(intervals, 2, 0))->unref();
128 SkPath fillPath;
129 dashPaint.getFillPath(fDashedfPath, &fillPath);
130 canvas->translate(0, 20);
131 canvas->drawPath(fDashedfPath, dashPaint);
132 canvas->translate(0, 20);
133 canvas->drawPath(fRefPath[i * 2], fillPaint);
134 strokePaint.setStrokeWidth(20);
135 strokePaint.setAlpha(127);
136 canvas->translate(0, 50);
137 canvas->drawPath(fMoveHfPath, strokePaint);
138 canvas->translate(0, 30);
139 canvas->drawPath(fMoveZfPath, strokePaint);
140 canvas->translate(0, 30);
141 fillPaint.setAlpha(127);
142 canvas->drawPath(fRefPath[1 + i * 2], fillPaint);
143 canvas->restore();
144 }
145 }
146
147private:
148 typedef skiagm::GM INHERITED;
149};
150
reed@google.com4384fab2012-06-05 16:14:23 +0000151class Strokes2GM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000152 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -0800153protected:
mtklein36352bf2015-03-25 18:17:31 -0700154 void onOnceBeforeDraw() override {
scroggof9d61012014-12-15 12:54:51 -0800155 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000156 fPath.moveTo(0, 0);
157 for (int i = 0; i < 13; i++) {
158 SkScalar x = rand.nextUScalar1() * (W >> 1);
159 SkScalar y = rand.nextUScalar1() * (H >> 1);
160 fPath.lineTo(x, y);
161 }
162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 SkString onShortName() override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000166 return SkString("strokes_poly");
167 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000168
mtklein36352bf2015-03-25 18:17:31 -0700169 SkISize onISize() override {
reed@google.com4384fab2012-06-05 16:14:23 +0000170 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000173 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
174 SkMatrix matrix;
175 matrix.setRotate(angle, px, py);
176 canvas->concat(matrix);
177 }
178
mtklein36352bf2015-03-25 18:17:31 -0700179 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000180 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000182 SkPaint paint;
183 paint.setStyle(SkPaint::kStroke_Style);
184 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000186 for (int y = 0; y < 2; y++) {
187 paint.setAntiAlias(!!y);
188 SkAutoCanvasRestore acr(canvas, true);
189 canvas->translate(0, SH * y);
190 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
191 SkIntToScalar(2),
192 SW - SkIntToScalar(2),
193 SH - SkIntToScalar(2)));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
scroggof9d61012014-12-15 12:54:51 -0800195 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000196 for (int i = 0; i < N/2; i++) {
197 SkRect r;
198 rnd_rect(&r, &paint, rand);
199 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
200 canvas->drawPath(fPath, paint);
201 }
202 }
203 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000205private:
reed@google.com4384fab2012-06-05 16:14:23 +0000206 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000207};
208
209//////////////////////////////////////////////////////////////////////////////
210
reed@google.com4384fab2012-06-05 16:14:23 +0000211static SkRect inset(const SkRect& r) {
212 SkRect rr(r);
213 rr.inset(r.width()/10, r.height()/10);
214 return rr;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000215}
216
reed@google.com4384fab2012-06-05 16:14:23 +0000217class Strokes3GM : public skiagm::GM {
218 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
219 path->addRect(bounds, SkPath::kCW_Direction);
220 path->addRect(inset(bounds), SkPath::kCW_Direction);
221 title->set("CW CW");
222 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000223
reed@google.com4384fab2012-06-05 16:14:23 +0000224 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
225 path->addRect(bounds, SkPath::kCW_Direction);
226 path->addRect(inset(bounds), SkPath::kCCW_Direction);
227 title->set("CW CCW");
228 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000229
reed@google.com4384fab2012-06-05 16:14:23 +0000230 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
231 path->addOval(bounds, SkPath::kCW_Direction);
232 path->addOval(inset(bounds), SkPath::kCW_Direction);
233 title->set("CW CW");
234 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000235
reed@google.com4384fab2012-06-05 16:14:23 +0000236 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
237 path->addOval(bounds, SkPath::kCW_Direction);
238 path->addOval(inset(bounds), SkPath::kCCW_Direction);
239 title->set("CW CCW");
240 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000241
reed@google.com4384fab2012-06-05 16:14:23 +0000242 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
243 path->addRect(bounds, SkPath::kCW_Direction);
244 SkRect r = bounds;
245 r.inset(bounds.width() / 10, -bounds.height() / 10);
246 path->addOval(r, SkPath::kCW_Direction);
247 title->set("CW CW");
248 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000249
reed@google.com4384fab2012-06-05 16:14:23 +0000250 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
251 path->addRect(bounds, SkPath::kCW_Direction);
252 SkRect r = bounds;
253 r.inset(bounds.width() / 10, -bounds.height() / 10);
254 path->addOval(r, SkPath::kCCW_Direction);
255 title->set("CW CCW");
256 }
257
258public:
259 Strokes3GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000260
reed@google.com4384fab2012-06-05 16:14:23 +0000261protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000262
mtklein36352bf2015-03-25 18:17:31 -0700263 SkString onShortName() override {
reed@google.com4384fab2012-06-05 16:14:23 +0000264 return SkString("strokes3");
265 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000266
mtklein36352bf2015-03-25 18:17:31 -0700267 SkISize onISize() override {
caryclark37604572015-02-23 06:51:04 -0800268 return SkISize::Make(1500, 1500);
reed@google.com4384fab2012-06-05 16:14:23 +0000269 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000270
mtklein36352bf2015-03-25 18:17:31 -0700271 void onDraw(SkCanvas* canvas) override {
reed@google.com4384fab2012-06-05 16:14:23 +0000272 SkPaint origPaint;
273 origPaint.setAntiAlias(true);
274 origPaint.setStyle(SkPaint::kStroke_Style);
275 SkPaint fillPaint(origPaint);
276 fillPaint.setColor(SK_ColorRED);
277 SkPaint strokePaint(origPaint);
caryclark12596012015-07-29 05:27:47 -0700278 strokePaint.setColor(sk_tool_utils::color_to_565(0xFF4444FF));
reed@google.com4384fab2012-06-05 16:14:23 +0000279
280 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
281 make0, make1, make2, make3, make4, make5
282 };
283
caryclark37604572015-02-23 06:51:04 -0800284 canvas->translate(SkIntToScalar(20), SkIntToScalar(80));
reed@google.com4384fab2012-06-05 16:14:23 +0000285
286 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
287 SkScalar dx = bounds.width() * 4/3;
288 SkScalar dy = bounds.height() * 5;
289
290 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
291 SkPath orig;
292 SkString str;
293 procs[i](&orig, bounds, &str);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000294
reed@google.com4384fab2012-06-05 16:14:23 +0000295 canvas->save();
296 for (int j = 0; j < 13; ++j) {
297 strokePaint.setStrokeWidth(SK_Scalar1 * j * j);
298 canvas->drawPath(orig, strokePaint);
299 canvas->drawPath(orig, origPaint);
300 SkPath fill;
301 strokePaint.getFillPath(orig, &fill);
302 canvas->drawPath(fill, fillPaint);
303 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
304 }
305 canvas->restore();
306 canvas->translate(0, dy);
307 }
308 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000309
reed@google.com4384fab2012-06-05 16:14:23 +0000310private:
311 typedef skiagm::GM INHERITED;
312};
313
caryclark612f70d2015-05-19 11:05:37 -0700314class Strokes4GM : public skiagm::GM {
315public:
316 Strokes4GM() {}
317
318protected:
319
320 SkString onShortName() override {
321 return SkString("strokes_zoomed");
322 }
323
324 SkISize onISize() override {
325 return SkISize::Make(W, H*2);
326 }
327
328 void onDraw(SkCanvas* canvas) override {
329 SkPaint paint;
330 paint.setStyle(SkPaint::kStroke_Style);
331 paint.setStrokeWidth(0.055f);
332
333 canvas->scale(1000, 1000);
334 canvas->drawCircle(0, 2, 1.97f, paint);
335 }
336
337private:
338 typedef skiagm::GM INHERITED;
339};
340
caryclark45398df2015-08-25 13:19:06 -0700341// Test stroking for curves that produce degenerate tangents when t is 0 or 1 (see bug 4191)
342class Strokes5GM : public skiagm::GM {
343public:
344 Strokes5GM() {}
345
346protected:
347
348 SkString onShortName() override {
349 return SkString("zero_control_stroke");
350 }
351
352 SkISize onISize() override {
353 return SkISize::Make(W, H*2);
354 }
355
356 void onDraw(SkCanvas* canvas) override {
357 SkPaint p;
358 p.setColor(SK_ColorRED);
359 p.setAntiAlias(true);
360 p.setStyle(SkPaint::kStroke_Style);
361 p.setStrokeWidth(40);
362 p.setStrokeCap(SkPaint::kButt_Cap);
363
364 SkPath path;
365 path.moveTo(157.474f,111.753f);
366 path.cubicTo(128.5f,111.5f,35.5f,29.5f,35.5f,29.5f);
367 canvas->drawPath(path, p);
368 path.reset();
369 path.moveTo(250, 50);
370 path.quadTo(280, 80, 280, 80);
371 canvas->drawPath(path, p);
372 path.reset();
373 path.moveTo(150, 50);
374 path.conicTo(180, 80, 180, 80, 0.707f);
375 canvas->drawPath(path, p);
376
377 path.reset();
378 path.moveTo(157.474f,311.753f);
379 path.cubicTo(157.474f,311.753f,85.5f,229.5f,35.5f,229.5f);
380 canvas->drawPath(path, p);
381 path.reset();
382 path.moveTo(280, 250);
383 path.quadTo(280, 250, 310, 280);
384 canvas->drawPath(path, p);
385 path.reset();
386 path.moveTo(180, 250);
387 path.conicTo(180, 250, 210, 280, 0.707f);
388 canvas->drawPath(path, p);
389 }
390
391private:
392 typedef skiagm::GM INHERITED;
393};
394
caryclark612f70d2015-05-19 11:05:37 -0700395
reed@google.com4384fab2012-06-05 16:14:23 +0000396//////////////////////////////////////////////////////////////////////////////
397
398static skiagm::GM* F0(void*) { return new StrokesGM; }
399static skiagm::GM* F1(void*) { return new Strokes2GM; }
400static skiagm::GM* F2(void*) { return new Strokes3GM; }
caryclark612f70d2015-05-19 11:05:37 -0700401static skiagm::GM* F3(void*) { return new Strokes4GM; }
caryclark45398df2015-08-25 13:19:06 -0700402static skiagm::GM* F4(void*) { return new Strokes5GM; }
reed@google.com4384fab2012-06-05 16:14:23 +0000403
404static skiagm::GMRegistry R0(F0);
405static skiagm::GMRegistry R1(F1);
406static skiagm::GMRegistry R2(F2);
caryclark612f70d2015-05-19 11:05:37 -0700407static skiagm::GMRegistry R3(F3);
caryclark45398df2015-08-25 13:19:06 -0700408static skiagm::GMRegistry R4(F4);
caryclark5cb00a92015-08-26 09:04:55 -0700409
410DEF_GM( return SkNEW(ZeroLenStrokesGM); )