blob: 81741693cfc765019bde77648b5b4b9ef2c7f96e [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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000021static void rnd_rect(SkRect* r, SkPaint* paint, SkLCGRandom& 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 virtual uint32_t onGetFlags() const SK_OVERRIDE {
43 return kSkipTiled_Flag;
44 }
45
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000046 virtual SkString onShortName() {
47 return SkString("strokes_round");
48 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000050 virtual SkISize onISize() {
reed@google.com4384fab2012-06-05 16:14:23 +000051 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000052 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
54 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000055 SkPaint paint;
56 paint.setStyle(SkPaint::kStroke_Style);
57 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000059 for (int y = 0; y < 2; y++) {
60 paint.setAntiAlias(!!y);
61 SkAutoCanvasRestore acr(canvas, true);
62 canvas->translate(0, SH * y);
63 canvas->clipRect(SkRect::MakeLTRB(
64 SkIntToScalar(2), SkIntToScalar(2)
65 , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
66 ));
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000068 SkLCGRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000069 for (int i = 0; i < N; i++) {
70 SkRect r;
71 rnd_rect(&r, &paint, rand);
72 canvas->drawOval(r, paint);
73 rnd_rect(&r, &paint, rand);
74 canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint);
75 rnd_rect(&r, &paint, rand);
76 }
77 }
78 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000080private:
reed@google.com4384fab2012-06-05 16:14:23 +000081 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000082};
83
reed@google.com4384fab2012-06-05 16:14:23 +000084class Strokes2GM : public skiagm::GM {
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000085 SkPath fPath;
86public:
87 Strokes2GM() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000088 SkLCGRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000089 fPath.moveTo(0, 0);
90 for (int i = 0; i < 13; i++) {
91 SkScalar x = rand.nextUScalar1() * (W >> 1);
92 SkScalar y = rand.nextUScalar1() * (H >> 1);
93 fPath.lineTo(x, y);
94 }
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +000097protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000098 virtual uint32_t onGetFlags() const SK_OVERRIDE {
99 return kSkipTiled_Flag;
100 }
101
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000102 virtual SkString onShortName() {
103 return SkString("strokes_poly");
104 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000106 virtual SkISize onISize() {
reed@google.com4384fab2012-06-05 16:14:23 +0000107 return SkISize::Make(W, H*2);
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000108 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000110 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
111 SkMatrix matrix;
112 matrix.setRotate(angle, px, py);
113 canvas->concat(matrix);
114 }
115
116 virtual void onDraw(SkCanvas* canvas) {
117 canvas->drawColor(SK_ColorWHITE);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000119 SkPaint paint;
120 paint.setStyle(SkPaint::kStroke_Style);
121 paint.setStrokeWidth(SkIntToScalar(9)/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000123 for (int y = 0; y < 2; y++) {
124 paint.setAntiAlias(!!y);
125 SkAutoCanvasRestore acr(canvas, true);
126 canvas->translate(0, SH * y);
127 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2),
128 SkIntToScalar(2),
129 SW - SkIntToScalar(2),
130 SH - SkIntToScalar(2)));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000132 SkLCGRandom rand;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000133 for (int i = 0; i < N/2; i++) {
134 SkRect r;
135 rnd_rect(&r, &paint, rand);
136 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
137 canvas->drawPath(fPath, paint);
138 }
139 }
140 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000142private:
reed@google.com4384fab2012-06-05 16:14:23 +0000143 typedef skiagm::GM INHERITED;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000144};
145
146//////////////////////////////////////////////////////////////////////////////
147
reed@google.com4384fab2012-06-05 16:14:23 +0000148static SkRect inset(const SkRect& r) {
149 SkRect rr(r);
150 rr.inset(r.width()/10, r.height()/10);
151 return rr;
mike@reedtribe.orgf2c21cd2011-06-18 00:15:04 +0000152}
153
reed@google.com4384fab2012-06-05 16:14:23 +0000154class Strokes3GM : public skiagm::GM {
155 static void make0(SkPath* path, const SkRect& bounds, SkString* title) {
156 path->addRect(bounds, SkPath::kCW_Direction);
157 path->addRect(inset(bounds), SkPath::kCW_Direction);
158 title->set("CW CW");
159 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
reed@google.com4384fab2012-06-05 16:14:23 +0000161 static void make1(SkPath* path, const SkRect& bounds, SkString* title) {
162 path->addRect(bounds, SkPath::kCW_Direction);
163 path->addRect(inset(bounds), SkPath::kCCW_Direction);
164 title->set("CW CCW");
165 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166
reed@google.com4384fab2012-06-05 16:14:23 +0000167 static void make2(SkPath* path, const SkRect& bounds, SkString* title) {
168 path->addOval(bounds, SkPath::kCW_Direction);
169 path->addOval(inset(bounds), SkPath::kCW_Direction);
170 title->set("CW CW");
171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed@google.com4384fab2012-06-05 16:14:23 +0000173 static void make3(SkPath* path, const SkRect& bounds, SkString* title) {
174 path->addOval(bounds, SkPath::kCW_Direction);
175 path->addOval(inset(bounds), SkPath::kCCW_Direction);
176 title->set("CW CCW");
177 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@google.com4384fab2012-06-05 16:14:23 +0000179 static void make4(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::kCW_Direction);
184 title->set("CW CW");
185 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
reed@google.com4384fab2012-06-05 16:14:23 +0000187 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
188 path->addRect(bounds, SkPath::kCW_Direction);
189 SkRect r = bounds;
190 r.inset(bounds.width() / 10, -bounds.height() / 10);
191 path->addOval(r, SkPath::kCCW_Direction);
192 title->set("CW CCW");
193 }
194
195public:
196 Strokes3GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197
reed@google.com4384fab2012-06-05 16:14:23 +0000198protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000199 virtual uint32_t onGetFlags() const SK_OVERRIDE {
200 return kSkipTiled_Flag;
201 }
202
reed@google.com4384fab2012-06-05 16:14:23 +0000203 virtual SkString onShortName() {
204 return SkString("strokes3");
205 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206
reed@google.com4384fab2012-06-05 16:14:23 +0000207 virtual SkISize onISize() {
208 return SkISize::Make(W, H*2);
209 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000210
reed@google.com4384fab2012-06-05 16:14:23 +0000211 virtual void onDraw(SkCanvas* canvas) {
212 SkPaint origPaint;
213 origPaint.setAntiAlias(true);
214 origPaint.setStyle(SkPaint::kStroke_Style);
215 SkPaint fillPaint(origPaint);
216 fillPaint.setColor(SK_ColorRED);
217 SkPaint strokePaint(origPaint);
218 strokePaint.setColor(0xFF4444FF);
219
220 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
221 make0, make1, make2, make3, make4, make5
222 };
223
224 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
225
226 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
227 SkScalar dx = bounds.width() * 4/3;
228 SkScalar dy = bounds.height() * 5;
229
230 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
231 SkPath orig;
232 SkString str;
233 procs[i](&orig, bounds, &str);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000234
reed@google.com4384fab2012-06-05 16:14:23 +0000235 canvas->save();
236 for (int j = 0; j < 13; ++j) {
237 strokePaint.setStrokeWidth(SK_Scalar1 * j * j);
238 canvas->drawPath(orig, strokePaint);
239 canvas->drawPath(orig, origPaint);
240 SkPath fill;
241 strokePaint.getFillPath(orig, &fill);
242 canvas->drawPath(fill, fillPaint);
243 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
244 }
245 canvas->restore();
246 canvas->translate(0, dy);
247 }
248 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000249
reed@google.com4384fab2012-06-05 16:14:23 +0000250private:
251 typedef skiagm::GM INHERITED;
252};
253
254//////////////////////////////////////////////////////////////////////////////
255
256static skiagm::GM* F0(void*) { return new StrokesGM; }
257static skiagm::GM* F1(void*) { return new Strokes2GM; }
258static skiagm::GM* F2(void*) { return new Strokes3GM; }
259
260static skiagm::GMRegistry R0(F0);
261static skiagm::GMRegistry R1(F1);
262static skiagm::GMRegistry R2(F2);