blob: 0bcb039783de0ce2de1380f57668a025514de7f7 [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"
11
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000012#define W 400
13#define H 400
14#define N 50
15
16static const SkScalar SW = SkIntToScalar(W);
17static const SkScalar SH = SkIntToScalar(H);
18
scroggof9d61012014-12-15 12:54:51 -080019static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000020 SkScalar x = rand.nextUScalar1() * W;
21 SkScalar y = rand.nextUScalar1() * H;
22 SkScalar w = rand.nextUScalar1() * (W >> 2);
23 SkScalar h = rand.nextUScalar1() * (H >> 2);
epoger@google.com17b78942011-08-26 14:40:38 +000024 SkScalar hoffset = rand.nextSScalar1();
25 SkScalar woffset = rand.nextSScalar1();
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000027 r->set(x, y, x + w, y + h);
epoger@google.com17b78942011-08-26 14:40:38 +000028 r->offset(-w/2 + woffset, -h/2 + hoffset);
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000030 paint->setColor(rand.nextU());
31 paint->setAlpha(0xFF);
32}
33
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@google.com4384fab2012-06-05 16:14:23 +000035class StrokesGM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000036public:
37 StrokesGM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000039protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000042 return SkString("strokes_round");
43 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
mtklein36352bf2015-03-25 18:17:31 -070045 SkISize onISize() override {
reed@google.com4384fab2012-06-05 16:14:23 +000046 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000047 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
mtklein36352bf2015-03-25 18:17:31 -070049 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000050 SkPaint paint;
51 paint.setStyle(SkPaint::kStroke_Style);
52 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000054 for (int y = 0; y < 2; y++) {
55 paint.setAntiAlias(!!y);
56 SkAutoCanvasRestore acr(canvas, true);
57 canvas->translate(0, SH * y);
58 canvas->clipRect(SkRect::MakeLTRB(
59 SkIntToScalar(2), SkIntToScalar(2)
60 , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
61 ));
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
scroggof9d61012014-12-15 12:54:51 -080063 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000064 for (int i = 0; i < N; i++) {
65 SkRect r;
66 rnd_rect(&r, &paint, rand);
67 canvas->drawOval(r, paint);
68 rnd_rect(&r, &paint, rand);
69 canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint);
70 rnd_rect(&r, &paint, rand);
71 }
72 }
73 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000075private:
reed@google.com4384fab2012-06-05 16:14:23 +000076 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000077};
78
reed@google.com4384fab2012-06-05 16:14:23 +000079class Strokes2GM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000080 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -080081protected:
mtklein36352bf2015-03-25 18:17:31 -070082 void onOnceBeforeDraw() override {
scroggof9d61012014-12-15 12:54:51 -080083 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000084 fPath.moveTo(0, 0);
85 for (int i = 0; i < 13; i++) {
86 SkScalar x = rand.nextUScalar1() * (W >> 1);
87 SkScalar y = rand.nextUScalar1() * (H >> 1);
88 fPath.lineTo(x, y);
89 }
90 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000092
mtklein36352bf2015-03-25 18:17:31 -070093 SkString onShortName() override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000094 return SkString("strokes_poly");
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
mtklein36352bf2015-03-25 18:17:31 -070097 SkISize onISize() override {
reed@google.com4384fab2012-06-05 16:14:23 +000098 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000099 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000101 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
102 SkMatrix matrix;
103 matrix.setRotate(angle, px, py);
104 canvas->concat(matrix);
105 }
106
mtklein36352bf2015-03-25 18:17:31 -0700107 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000108 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000110 SkPaint paint;
111 paint.setStyle(SkPaint::kStroke_Style);
112 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000114 for (int y = 0; y < 2; y++) {
115 paint.setAntiAlias(!!y);
116 SkAutoCanvasRestore acr(canvas, true);
117 canvas->translate(0, SH * y);
118 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
119 SkIntToScalar(2),
120 SW - SkIntToScalar(2),
121 SH - SkIntToScalar(2)));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
scroggof9d61012014-12-15 12:54:51 -0800123 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000124 for (int i = 0; i < N/2; i++) {
125 SkRect r;
126 rnd_rect(&r, &paint, rand);
127 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
128 canvas->drawPath(fPath, paint);
129 }
130 }
131 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000133private:
reed@google.com4384fab2012-06-05 16:14:23 +0000134 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000135};
136
137//////////////////////////////////////////////////////////////////////////////
138
reed@google.com4384fab2012-06-05 16:14:23 +0000139static SkRect inset(const SkRect& r) {
140 SkRect rr(r);
141 rr.inset(r.width()/10, r.height()/10);
142 return rr;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000143}
144
reed@google.com4384fab2012-06-05 16:14:23 +0000145class Strokes3GM : public skiagm::GM {
146 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
147 path->addRect(bounds, SkPath::kCW_Direction);
148 path->addRect(inset(bounds), SkPath::kCW_Direction);
149 title->set("CW CW");
150 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@google.com4384fab2012-06-05 16:14:23 +0000152 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
153 path->addRect(bounds, SkPath::kCW_Direction);
154 path->addRect(inset(bounds), SkPath::kCCW_Direction);
155 title->set("CW CCW");
156 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@google.com4384fab2012-06-05 16:14:23 +0000158 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
159 path->addOval(bounds, SkPath::kCW_Direction);
160 path->addOval(inset(bounds), SkPath::kCW_Direction);
161 title->set("CW CW");
162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@google.com4384fab2012-06-05 16:14:23 +0000164 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
165 path->addOval(bounds, SkPath::kCW_Direction);
166 path->addOval(inset(bounds), SkPath::kCCW_Direction);
167 title->set("CW CCW");
168 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
reed@google.com4384fab2012-06-05 16:14:23 +0000170 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
171 path->addRect(bounds, SkPath::kCW_Direction);
172 SkRect r = bounds;
173 r.inset(bounds.width() / 10, -bounds.height() / 10);
174 path->addOval(r, SkPath::kCW_Direction);
175 title->set("CW CW");
176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
reed@google.com4384fab2012-06-05 16:14:23 +0000178 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
179 path->addRect(bounds, SkPath::kCW_Direction);
180 SkRect r = bounds;
181 r.inset(bounds.width() / 10, -bounds.height() / 10);
182 path->addOval(r, SkPath::kCCW_Direction);
183 title->set("CW CCW");
184 }
185
186public:
187 Strokes3GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000188
reed@google.com4384fab2012-06-05 16:14:23 +0000189protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000190
mtklein36352bf2015-03-25 18:17:31 -0700191 SkString onShortName() override {
reed@google.com4384fab2012-06-05 16:14:23 +0000192 return SkString("strokes3");
193 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
mtklein36352bf2015-03-25 18:17:31 -0700195 SkISize onISize() override {
caryclark37604572015-02-23 06:51:04 -0800196 return SkISize::Make(1500, 1500);
reed@google.com4384fab2012-06-05 16:14:23 +0000197 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
mtklein36352bf2015-03-25 18:17:31 -0700199 void onDraw(SkCanvas* canvas) override {
reed@google.com4384fab2012-06-05 16:14:23 +0000200 SkPaint origPaint;
201 origPaint.setAntiAlias(true);
202 origPaint.setStyle(SkPaint::kStroke_Style);
203 SkPaint fillPaint(origPaint);
204 fillPaint.setColor(SK_ColorRED);
205 SkPaint strokePaint(origPaint);
caryclark12596012015-07-29 05:27:47 -0700206 strokePaint.setColor(sk_tool_utils::color_to_565(0xFF4444FF));
reed@google.com4384fab2012-06-05 16:14:23 +0000207
208 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
209 make0, make1, make2, make3, make4, make5
210 };
211
caryclark37604572015-02-23 06:51:04 -0800212 canvas->translate(SkIntToScalar(20), SkIntToScalar(80));
reed@google.com4384fab2012-06-05 16:14:23 +0000213
214 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
215 SkScalar dx = bounds.width() * 4/3;
216 SkScalar dy = bounds.height() * 5;
217
218 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
219 SkPath orig;
220 SkString str;
221 procs[i](&orig, bounds, &str);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000222
reed@google.com4384fab2012-06-05 16:14:23 +0000223 canvas->save();
224 for (int j = 0; j < 13; ++j) {
225 strokePaint.setStrokeWidth(SK_Scalar1 * j * j);
226 canvas->drawPath(orig, strokePaint);
227 canvas->drawPath(orig, origPaint);
228 SkPath fill;
229 strokePaint.getFillPath(orig, &fill);
230 canvas->drawPath(fill, fillPaint);
231 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
232 }
233 canvas->restore();
234 canvas->translate(0, dy);
235 }
236 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000237
reed@google.com4384fab2012-06-05 16:14:23 +0000238private:
239 typedef skiagm::GM INHERITED;
240};
241
caryclark612f70d2015-05-19 11:05:37 -0700242class Strokes4GM : public skiagm::GM {
243public:
244 Strokes4GM() {}
245
246protected:
247
248 SkString onShortName() override {
249 return SkString("strokes_zoomed");
250 }
251
252 SkISize onISize() override {
253 return SkISize::Make(W, H*2);
254 }
255
256 void onDraw(SkCanvas* canvas) override {
257 SkPaint paint;
258 paint.setStyle(SkPaint::kStroke_Style);
259 paint.setStrokeWidth(0.055f);
260
261 canvas->scale(1000, 1000);
262 canvas->drawCircle(0, 2, 1.97f, paint);
263 }
264
265private:
266 typedef skiagm::GM INHERITED;
267};
268
caryclark45398df2015-08-25 13:19:06 -0700269// Test stroking for curves that produce degenerate tangents when t is 0 or 1 (see bug 4191)
270class Strokes5GM : public skiagm::GM {
271public:
272 Strokes5GM() {}
273
274protected:
275
276 SkString onShortName() override {
277 return SkString("zero_control_stroke");
278 }
279
280 SkISize onISize() override {
281 return SkISize::Make(W, H*2);
282 }
283
284 void onDraw(SkCanvas* canvas) override {
285 SkPaint p;
286 p.setColor(SK_ColorRED);
287 p.setAntiAlias(true);
288 p.setStyle(SkPaint::kStroke_Style);
289 p.setStrokeWidth(40);
290 p.setStrokeCap(SkPaint::kButt_Cap);
291
292 SkPath path;
293 path.moveTo(157.474f,111.753f);
294 path.cubicTo(128.5f,111.5f,35.5f,29.5f,35.5f,29.5f);
295 canvas->drawPath(path, p);
296 path.reset();
297 path.moveTo(250, 50);
298 path.quadTo(280, 80, 280, 80);
299 canvas->drawPath(path, p);
300 path.reset();
301 path.moveTo(150, 50);
302 path.conicTo(180, 80, 180, 80, 0.707f);
303 canvas->drawPath(path, p);
304
305 path.reset();
306 path.moveTo(157.474f,311.753f);
307 path.cubicTo(157.474f,311.753f,85.5f,229.5f,35.5f,229.5f);
308 canvas->drawPath(path, p);
309 path.reset();
310 path.moveTo(280, 250);
311 path.quadTo(280, 250, 310, 280);
312 canvas->drawPath(path, p);
313 path.reset();
314 path.moveTo(180, 250);
315 path.conicTo(180, 250, 210, 280, 0.707f);
316 canvas->drawPath(path, p);
317 }
318
319private:
320 typedef skiagm::GM INHERITED;
321};
322
caryclark612f70d2015-05-19 11:05:37 -0700323
reed@google.com4384fab2012-06-05 16:14:23 +0000324//////////////////////////////////////////////////////////////////////////////
325
326static skiagm::GM* F0(void*) { return new StrokesGM; }
327static skiagm::GM* F1(void*) { return new Strokes2GM; }
328static skiagm::GM* F2(void*) { return new Strokes3GM; }
caryclark612f70d2015-05-19 11:05:37 -0700329static skiagm::GM* F3(void*) { return new Strokes4GM; }
caryclark45398df2015-08-25 13:19:06 -0700330static skiagm::GM* F4(void*) { return new Strokes5GM; }
reed@google.com4384fab2012-06-05 16:14:23 +0000331
332static skiagm::GMRegistry R0(F0);
333static skiagm::GMRegistry R1(F1);
334static skiagm::GMRegistry R2(F2);
caryclark612f70d2015-05-19 11:05:37 -0700335static skiagm::GMRegistry R3(F3);
caryclark45398df2015-08-25 13:19:06 -0700336static skiagm::GMRegistry R4(F4);