blob: a258dfe0059a1f562fcbef092a61a5cc528d64fc [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
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
reed@google.com4384fab2012-06-05 16:14:23 +000081class Strokes2GM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000082 SkPath fPath;
caryclark63c684a2015-02-25 09:04:04 -080083protected:
mtklein36352bf2015-03-25 18:17:31 -070084 void onOnceBeforeDraw() override {
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
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000094
mtklein36352bf2015-03-25 18:17:31 -070095 SkString onShortName() override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000096 return SkString("strokes_poly");
97 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
mtklein36352bf2015-03-25 18:17:31 -070099 SkISize onISize() override {
reed@google.com4384fab2012-06-05 16:14:23 +0000100 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000101 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000103 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
104 SkMatrix matrix;
105 matrix.setRotate(angle, px, py);
106 canvas->concat(matrix);
107 }
108
mtklein36352bf2015-03-25 18:17:31 -0700109 void onDraw(SkCanvas* canvas) override {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000110 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000112 SkPaint paint;
113 paint.setStyle(SkPaint::kStroke_Style);
114 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000116 for (int y = 0; y < 2; y++) {
117 paint.setAntiAlias(!!y);
118 SkAutoCanvasRestore acr(canvas, true);
119 canvas->translate(0, SH * y);
120 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
121 SkIntToScalar(2),
122 SW - SkIntToScalar(2),
123 SH - SkIntToScalar(2)));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
scroggof9d61012014-12-15 12:54:51 -0800125 SkRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000126 for (int i = 0; i < N/2; i++) {
127 SkRect r;
128 rnd_rect(&r, &paint, rand);
129 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
130 canvas->drawPath(fPath, paint);
131 }
132 }
133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000135private:
reed@google.com4384fab2012-06-05 16:14:23 +0000136 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000137};
138
139//////////////////////////////////////////////////////////////////////////////
140
reed@google.com4384fab2012-06-05 16:14:23 +0000141static SkRect inset(const SkRect& r) {
142 SkRect rr(r);
143 rr.inset(r.width()/10, r.height()/10);
144 return rr;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000145}
146
reed@google.com4384fab2012-06-05 16:14:23 +0000147class Strokes3GM : public skiagm::GM {
148 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
149 path->addRect(bounds, SkPath::kCW_Direction);
150 path->addRect(inset(bounds), SkPath::kCW_Direction);
151 title->set("CW CW");
152 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@google.com4384fab2012-06-05 16:14:23 +0000154 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
155 path->addRect(bounds, SkPath::kCW_Direction);
156 path->addRect(inset(bounds), SkPath::kCCW_Direction);
157 title->set("CW CCW");
158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@google.com4384fab2012-06-05 16:14:23 +0000160 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
161 path->addOval(bounds, SkPath::kCW_Direction);
162 path->addOval(inset(bounds), SkPath::kCW_Direction);
163 title->set("CW CW");
164 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
reed@google.com4384fab2012-06-05 16:14:23 +0000166 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
167 path->addOval(bounds, SkPath::kCW_Direction);
168 path->addOval(inset(bounds), SkPath::kCCW_Direction);
169 title->set("CW CCW");
170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
reed@google.com4384fab2012-06-05 16:14:23 +0000172 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
173 path->addRect(bounds, SkPath::kCW_Direction);
174 SkRect r = bounds;
175 r.inset(bounds.width() / 10, -bounds.height() / 10);
176 path->addOval(r, SkPath::kCW_Direction);
177 title->set("CW CW");
178 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@google.com4384fab2012-06-05 16:14:23 +0000180 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
181 path->addRect(bounds, SkPath::kCW_Direction);
182 SkRect r = bounds;
183 r.inset(bounds.width() / 10, -bounds.height() / 10);
184 path->addOval(r, SkPath::kCCW_Direction);
185 title->set("CW CCW");
186 }
187
188public:
189 Strokes3GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
reed@google.com4384fab2012-06-05 16:14:23 +0000191protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000192
mtklein36352bf2015-03-25 18:17:31 -0700193 SkString onShortName() override {
reed@google.com4384fab2012-06-05 16:14:23 +0000194 return SkString("strokes3");
195 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
mtklein36352bf2015-03-25 18:17:31 -0700197 SkISize onISize() override {
caryclark37604572015-02-23 06:51:04 -0800198 return SkISize::Make(1500, 1500);
reed@google.com4384fab2012-06-05 16:14:23 +0000199 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200
mtklein36352bf2015-03-25 18:17:31 -0700201 void onDraw(SkCanvas* canvas) override {
reed@google.com4384fab2012-06-05 16:14:23 +0000202 SkPaint origPaint;
203 origPaint.setAntiAlias(true);
204 origPaint.setStyle(SkPaint::kStroke_Style);
205 SkPaint fillPaint(origPaint);
206 fillPaint.setColor(SK_ColorRED);
207 SkPaint strokePaint(origPaint);
208 strokePaint.setColor(0xFF4444FF);
209
210 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
211 make0, make1, make2, make3, make4, make5
212 };
213
caryclark37604572015-02-23 06:51:04 -0800214 canvas->translate(SkIntToScalar(20), SkIntToScalar(80));
reed@google.com4384fab2012-06-05 16:14:23 +0000215
216 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
217 SkScalar dx = bounds.width() * 4/3;
218 SkScalar dy = bounds.height() * 5;
219
220 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
221 SkPath orig;
222 SkString str;
223 procs[i](&orig, bounds, &str);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000224
reed@google.com4384fab2012-06-05 16:14:23 +0000225 canvas->save();
226 for (int j = 0; j < 13; ++j) {
227 strokePaint.setStrokeWidth(SK_Scalar1 * j * j);
228 canvas->drawPath(orig, strokePaint);
229 canvas->drawPath(orig, origPaint);
230 SkPath fill;
231 strokePaint.getFillPath(orig, &fill);
232 canvas->drawPath(fill, fillPaint);
233 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
234 }
235 canvas->restore();
236 canvas->translate(0, dy);
237 }
238 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000239
reed@google.com4384fab2012-06-05 16:14:23 +0000240private:
241 typedef skiagm::GM INHERITED;
242};
243
244//////////////////////////////////////////////////////////////////////////////
245
246static skiagm::GM* F0(void*) { return new StrokesGM; }
247static skiagm::GM* F1(void*) { return new Strokes2GM; }
248static skiagm::GM* F2(void*) { return new Strokes3GM; }
249
250static skiagm::GMRegistry R0(F0);
251static skiagm::GMRegistry R1(F1);
252static skiagm::GMRegistry R2(F2);