3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
18 static const SkScalar SW = SkIntToScalar(W);
19 static const SkScalar SH = SkIntToScalar(H);
21 static 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);
26 SkScalar hoffset = rand.nextSScalar1();
27 SkScalar woffset = rand.nextSScalar1();
29 r->set(x, y, x + w, y + h);
30 r->offset(-w/2 + woffset, -h/2 + hoffset);
32 paint->setColor(rand.nextU());
33 paint->setAlpha(0xFF);
37 class StrokesGM : public skiagm::GM {
43 SkString onShortName() SK_OVERRIDE {
44 return SkString("strokes_round");
47 SkISize onISize() SK_OVERRIDE {
48 return SkISize::Make(W, H*2);
51 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
53 paint.setStyle(SkPaint::kStroke_Style);
54 paint.setStrokeWidth(SkIntToScalar(9)/2);
56 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)
66 for (int i = 0; i < N; i++) {
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);
78 typedef skiagm::GM INHERITED;
81 class Strokes2GM : public skiagm::GM {
84 void onOnceBeforeDraw() SK_OVERRIDE {
87 for (int i = 0; i < 13; i++) {
88 SkScalar x = rand.nextUScalar1() * (W >> 1);
89 SkScalar y = rand.nextUScalar1() * (H >> 1);
95 SkString onShortName() SK_OVERRIDE {
96 return SkString("strokes_poly");
99 SkISize onISize() SK_OVERRIDE {
100 return SkISize::Make(W, H*2);
103 static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) {
105 matrix.setRotate(angle, px, py);
106 canvas->concat(matrix);
109 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
110 canvas->drawColor(SK_ColorWHITE);
113 paint.setStyle(SkPaint::kStroke_Style);
114 paint.setStrokeWidth(SkIntToScalar(9)/2);
116 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),
122 SW - SkIntToScalar(2),
123 SH - SkIntToScalar(2)));
126 for (int i = 0; i < N/2; i++) {
128 rnd_rect(&r, &paint, rand);
129 rotate(SkIntToScalar(15), SW/2, SH/2, canvas);
130 canvas->drawPath(fPath, paint);
136 typedef skiagm::GM INHERITED;
139 //////////////////////////////////////////////////////////////////////////////
141 static SkRect inset(const SkRect& r) {
143 rr.inset(r.width()/10, r.height()/10);
147 class 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);
154 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");
160 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);
166 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");
172 static void make4(SkPath* path, const SkRect& bounds, SkString* title) {
173 path->addRect(bounds, SkPath::kCW_Direction);
175 r.inset(bounds.width() / 10, -bounds.height() / 10);
176 path->addOval(r, SkPath::kCW_Direction);
180 static void make5(SkPath* path, const SkRect& bounds, SkString* title) {
181 path->addRect(bounds, SkPath::kCW_Direction);
183 r.inset(bounds.width() / 10, -bounds.height() / 10);
184 path->addOval(r, SkPath::kCCW_Direction);
185 title->set("CW CCW");
193 SkString onShortName() SK_OVERRIDE {
194 return SkString("strokes3");
197 SkISize onISize() SK_OVERRIDE {
198 return SkISize::Make(1500, 1500);
201 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
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);
210 void (*procs[])(SkPath*, const SkRect&, SkString*) = {
211 make0, make1, make2, make3, make4, make5
214 canvas->translate(SkIntToScalar(20), SkIntToScalar(80));
216 SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50));
217 SkScalar dx = bounds.width() * 4/3;
218 SkScalar dy = bounds.height() * 5;
220 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
223 procs[i](&orig, bounds, &str);
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);
231 strokePaint.getFillPath(orig, &fill);
232 canvas->drawPath(fill, fillPaint);
233 canvas->translate(dx + strokePaint.getStrokeWidth(), 0);
236 canvas->translate(0, dy);
241 typedef skiagm::GM INHERITED;
244 //////////////////////////////////////////////////////////////////////////////
246 static skiagm::GM* F0(void*) { return new StrokesGM; }
247 static skiagm::GM* F1(void*) { return new Strokes2GM; }
248 static skiagm::GM* F2(void*) { return new Strokes3GM; }
250 static skiagm::GMRegistry R0(F0);
251 static skiagm::GMRegistry R1(F1);
252 static skiagm::GMRegistry R2(F2);