blob: 49a7fc63b487e8adf055ba9886df76b4a68ee57b [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
21static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) {
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.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:
42 virtual SkString onShortName() {
43 return SkString("strokes_round");
44 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000046 virtual SkISize onISize() {
reed@google.com4384fab2012-06-05 16:14:23 +000047 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000048 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
50 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000051 SkPaint paint;
52 paint.setStyle(SkPaint::kStroke_Style);
53 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000055 for (int y = 0; y < 2; y++) {
56 paint.setAntiAlias(!!y);
57 SkAutoCanvasRestore acr(canvas, true);
58 canvas->translate(0, SH * y);
59 canvas->clipRect(SkRect::MakeLTRB(
60 SkIntToScalar(2), SkIntToScalar(2)
61 , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
62 ));
rmistry@google.comae933ce2012-08-23 18:19:56 +000063
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000064 SkRandom rand;
65 for (int i = 0; i < N; i++) {
66 SkRect r;
67 rnd_rect(&r, &paint, rand);
68 canvas->drawOval(r, paint);
69 rnd_rect(&r, &paint, rand);
70 canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint);
71 rnd_rect(&r, &paint, rand);
72 }
73 }
74 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000076private:
reed@google.com4384fab2012-06-05 16:14:23 +000077 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000078};
79
reed@google.com4384fab2012-06-05 16:14:23 +000080class Strokes2GM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000081 SkPath fPath;
82public:
83 Strokes2GM() {
84 SkRandom rand;
85 fPath.moveTo(0, 0);
86 for (int i = 0; i < 13; i++) {
87 SkScalar x = rand.nextUScalar1() * (W >> 1);
88 SkScalar y = rand.nextUScalar1() * (H >> 1);
89 fPath.lineTo(x, y);
90 }
91 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000093protected:
94 virtual SkString onShortName() {
95 return SkString("strokes_poly");
96 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000098 virtual SkISize onISize() {
reed@google.com4384fab2012-06-05 16:14:23 +000099 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000100 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000102 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
103 SkMatrix matrix;
104 matrix.setRotate(angle, px, py);
105 canvas->concat(matrix);
106 }
107
108 virtual void onDraw(SkCanvas* canvas) {
109 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000111 SkPaint paint;
112 paint.setStyle(SkPaint::kStroke_Style);
113 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000115 for (int y = 0; y < 2; y++) {
116 paint.setAntiAlias(!!y);
117 SkAutoCanvasRestore acr(canvas, true);
118 canvas->translate(0, SH * y);
119 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
120 SkIntToScalar(2),
121 SW - SkIntToScalar(2),
122 SH - SkIntToScalar(2)));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000124 SkRandom rand;
125 for (int i = 0; i < N/2; i++) {
126 SkRect r;
127 rnd_rect(&r, &paint, rand);
128 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
129 canvas->drawPath(fPath, paint);
130 }
131 }
132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000134private:
reed@google.com4384fab2012-06-05 16:14:23 +0000135 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000136};
137
138//////////////////////////////////////////////////////////////////////////////
139
reed@google.com4384fab2012-06-05 16:14:23 +0000140static SkRect inset(const SkRect& r) {
141 SkRect rr(r);
142 rr.inset(r.width()/10, r.height()/10);
143 return rr;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000144}
145
reed@google.com4384fab2012-06-05 16:14:23 +0000146class Strokes3GM : public skiagm::GM {
147 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
148 path->addRect(bounds, SkPath::kCW_Direction);
149 path->addRect(inset(bounds), SkPath::kCW_Direction);
150 title->set("CW CW");
151 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152
reed@google.com4384fab2012-06-05 16:14:23 +0000153 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
154 path->addRect(bounds, SkPath::kCW_Direction);
155 path->addRect(inset(bounds), SkPath::kCCW_Direction);
156 title->set("CW CCW");
157 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@google.com4384fab2012-06-05 16:14:23 +0000159 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
160 path->addOval(bounds, SkPath::kCW_Direction);
161 path->addOval(inset(bounds), SkPath::kCW_Direction);
162 title->set("CW CW");
163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@google.com4384fab2012-06-05 16:14:23 +0000165 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
166 path->addOval(bounds, SkPath::kCW_Direction);
167 path->addOval(inset(bounds), SkPath::kCCW_Direction);
168 title->set("CW CCW");
169 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000170
reed@google.com4384fab2012-06-05 16:14:23 +0000171 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
172 path->addRect(bounds, SkPath::kCW_Direction);
173 SkRect r = bounds;
174 r.inset(bounds.width() / 10, -bounds.height() / 10);
175 path->addOval(r, SkPath::kCW_Direction);
176 title->set("CW CW");
177 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@google.com4384fab2012-06-05 16:14:23 +0000179 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
180 path->addRect(bounds, SkPath::kCW_Direction);
181 SkRect r = bounds;
182 r.inset(bounds.width() / 10, -bounds.height() / 10);
183 path->addOval(r, SkPath::kCCW_Direction);
184 title->set("CW CCW");
185 }
186
187public:
188 Strokes3GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000189
reed@google.com4384fab2012-06-05 16:14:23 +0000190protected:
191 virtual SkString onShortName() {
192 return SkString("strokes3");
193 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
reed@google.com4384fab2012-06-05 16:14:23 +0000195 virtual SkISize onISize() {
196 return SkISize::Make(W, H*2);
197 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
reed@google.com4384fab2012-06-05 16:14:23 +0000199 virtual void onDraw(SkCanvas* canvas) {
200 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);
206 strokePaint.setColor(0xFF4444FF);
207
208 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
209 make0, make1, make2, make3, make4, make5
210 };
211
212 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
213
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
242//////////////////////////////////////////////////////////////////////////////
243
244static skiagm::GM* F0(void*) { return new StrokesGM; }
245static skiagm::GM* F1(void*) { return new Strokes2GM; }
246static skiagm::GM* F2(void*) { return new Strokes3GM; }
247
248static skiagm::GMRegistry R0(F0);
249static skiagm::GMRegistry R1(F1);
250static skiagm::GMRegistry R2(F2);