blob: 1039769e0b32b6251db476709247455589ca0af7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000011#include "gm.h"
12#include "SkRandom.h"
13
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
mtklein72c9faa2015-01-09 10:06:39 -080043 SkString onShortName() SK_OVERRIDE {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000044 return SkString("strokes_round");
45 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
mtklein72c9faa2015-01-09 10:06:39 -080047 SkISize onISize() SK_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
mtklein72c9faa2015-01-09 10:06:39 -080051 void onDraw(SkCanvas* canvas) SK_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
reed@google.com4384fab2012-06-05 16:14:23 +000081class Strokes2GM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000082 SkPath fPath;
83public:
84 Strokes2GM() {
scroggof9d61012014-12-15 12:54:51 -080085 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000086 fPath.moveTo(0, 0);
87 for (int i = 0; i < 13; i++) {
88 SkScalar x = rand.nextUScalar1() * (W >> 1);
89 SkScalar y = rand.nextUScalar1() * (H >> 1);
90 fPath.lineTo(x, y);
91 }
92 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000094protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000095
mtklein72c9faa2015-01-09 10:06:39 -080096 SkString onShortName() SK_OVERRIDE {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000097 return SkString("strokes_poly");
98 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
mtklein72c9faa2015-01-09 10:06:39 -0800100 SkISize onISize() SK_OVERRIDE {
reed@google.com4384fab2012-06-05 16:14:23 +0000101 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000102 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000104 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
105 SkMatrix matrix;
106 matrix.setRotate(angle, px, py);
107 canvas->concat(matrix);
108 }
109
mtklein72c9faa2015-01-09 10:06:39 -0800110 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000111 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000113 SkPaint paint;
114 paint.setStyle(SkPaint::kStroke_Style);
115 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000117 for (int y = 0; y < 2; y++) {
118 paint.setAntiAlias(!!y);
119 SkAutoCanvasRestore acr(canvas, true);
120 canvas->translate(0, SH * y);
121 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
122 SkIntToScalar(2),
123 SW - SkIntToScalar(2),
124 SH - SkIntToScalar(2)));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
scroggof9d61012014-12-15 12:54:51 -0800126 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000127 for (int i = 0; i < N/2; i++) {
128 SkRect r;
129 rnd_rect(&r, &paint, rand);
130 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
131 canvas->drawPath(fPath, paint);
132 }
133 }
134 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000136private:
reed@google.com4384fab2012-06-05 16:14:23 +0000137 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000138};
139
140//////////////////////////////////////////////////////////////////////////////
141
reed@google.com4384fab2012-06-05 16:14:23 +0000142static SkRect inset(const SkRect& r) {
143 SkRect rr(r);
144 rr.inset(r.width()/10, r.height()/10);
145 return rr;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000146}
147
reed@google.com4384fab2012-06-05 16:14:23 +0000148class Strokes3GM : public skiagm::GM {
149 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
150 path->addRect(bounds, SkPath::kCW_Direction);
151 path->addRect(inset(bounds), SkPath::kCW_Direction);
152 title->set("CW CW");
153 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
reed@google.com4384fab2012-06-05 16:14:23 +0000155 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
156 path->addRect(bounds, SkPath::kCW_Direction);
157 path->addRect(inset(bounds), SkPath::kCCW_Direction);
158 title->set("CW CCW");
159 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
reed@google.com4384fab2012-06-05 16:14:23 +0000161 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
162 path->addOval(bounds, SkPath::kCW_Direction);
163 path->addOval(inset(bounds), SkPath::kCW_Direction);
164 title->set("CW CW");
165 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
reed@google.com4384fab2012-06-05 16:14:23 +0000167 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
168 path->addOval(bounds, SkPath::kCW_Direction);
169 path->addOval(inset(bounds), SkPath::kCCW_Direction);
170 title->set("CW CCW");
171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed@google.com4384fab2012-06-05 16:14:23 +0000173 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
174 path->addRect(bounds, SkPath::kCW_Direction);
175 SkRect r = bounds;
176 r.inset(bounds.width() / 10, -bounds.height() / 10);
177 path->addOval(r, SkPath::kCW_Direction);
178 title->set("CW CW");
179 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed@google.com4384fab2012-06-05 16:14:23 +0000181 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
182 path->addRect(bounds, SkPath::kCW_Direction);
183 SkRect r = bounds;
184 r.inset(bounds.width() / 10, -bounds.height() / 10);
185 path->addOval(r, SkPath::kCCW_Direction);
186 title->set("CW CCW");
187 }
188
189public:
190 Strokes3GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed@google.com4384fab2012-06-05 16:14:23 +0000192protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000193
mtklein72c9faa2015-01-09 10:06:39 -0800194 SkString onShortName() SK_OVERRIDE {
reed@google.com4384fab2012-06-05 16:14:23 +0000195 return SkString("strokes3");
196 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
mtklein72c9faa2015-01-09 10:06:39 -0800198 SkISize onISize() SK_OVERRIDE {
reed@google.com4384fab2012-06-05 16:14:23 +0000199 return SkISize::Make(W, H*2);
200 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
mtklein72c9faa2015-01-09 10:06:39 -0800202 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com4384fab2012-06-05 16:14:23 +0000203 SkPaint origPaint;
204 origPaint.setAntiAlias(true);
205 origPaint.setStyle(SkPaint::kStroke_Style);
206 SkPaint fillPaint(origPaint);
207 fillPaint.setColor(SK_ColorRED);
208 SkPaint strokePaint(origPaint);
209 strokePaint.setColor(0xFF4444FF);
210
211 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
212 make0, make1, make2, make3, make4, make5
213 };
214
215 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
216
217 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
218 SkScalar dx = bounds.width() * 4/3;
219 SkScalar dy = bounds.height() * 5;
220
221 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
222 SkPath orig;
223 SkString str;
224 procs[i](&orig, bounds, &str);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000225
reed@google.com4384fab2012-06-05 16:14:23 +0000226 canvas->save();
227 for (int j = 0; j < 13; ++j) {
228 strokePaint.setStrokeWidth(SK_Scalar1 * j * j);
229 canvas->drawPath(orig, strokePaint);
230 canvas->drawPath(orig, origPaint);
231 SkPath fill;
232 strokePaint.getFillPath(orig, &fill);
233 canvas->drawPath(fill, fillPaint);
234 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
235 }
236 canvas->restore();
237 canvas->translate(0, dy);
238 }
239 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000240
reed@google.com4384fab2012-06-05 16:14:23 +0000241private:
242 typedef skiagm::GM INHERITED;
243};
244
245//////////////////////////////////////////////////////////////////////////////
246
247static skiagm::GM* F0(void*) { return new StrokesGM; }
248static skiagm::GM* F1(void*) { return new Strokes2GM; }
249static skiagm::GM* F2(void*) { return new Strokes3GM; }
250
251static skiagm::GMRegistry R0(F0);
252static skiagm::GMRegistry R1(F1);
253static skiagm::GMRegistry R2(F2);