1
2/*
3 * 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.
7 */
8
9
10
11#include "gm.h"
12#include "SkRandom.h"
13
14namespace skiagm {
15
16#define W   400
17#define H   400
18#define N   50
19
20static const SkScalar SW = SkIntToScalar(W);
21static const SkScalar SH = SkIntToScalar(H);
22
23static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) {
24    SkScalar x = rand.nextUScalar1() * W;
25    SkScalar y = rand.nextUScalar1() * H;
26    SkScalar w = rand.nextUScalar1() * (W >> 2);
27    SkScalar h = rand.nextUScalar1() * (H >> 2);
28    SkScalar hoffset = rand.nextSScalar1();
29    SkScalar woffset = rand.nextSScalar1();
30
31    r->set(x, y, x + w, y + h);
32    r->offset(-w/2 + woffset, -h/2 + hoffset);
33
34    paint->setColor(rand.nextU());
35    paint->setAlpha(0xFF);
36}
37
38
39class StrokesGM : public GM {
40public:
41    StrokesGM() {}
42
43protected:
44    virtual SkString onShortName() {
45        return SkString("strokes_round");
46    }
47
48    virtual SkISize onISize() {
49        return make_isize(W, H*2);
50    }
51
52    virtual void onDraw(SkCanvas* canvas) {
53        SkPaint paint;
54        paint.setStyle(SkPaint::kStroke_Style);
55        paint.setStrokeWidth(SkIntToScalar(9)/2);
56
57        for (int y = 0; y < 2; y++) {
58            paint.setAntiAlias(!!y);
59            SkAutoCanvasRestore acr(canvas, true);
60            canvas->translate(0, SH * y);
61            canvas->clipRect(SkRect::MakeLTRB(
62                                              SkIntToScalar(2), SkIntToScalar(2)
63                                              , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
64                                              ));
65
66            SkRandom rand;
67            for (int i = 0; i < N; i++) {
68                SkRect r;
69                rnd_rect(&r, &paint, rand);
70                canvas->drawOval(r, paint);
71                rnd_rect(&r, &paint, rand);
72                canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint);
73                rnd_rect(&r, &paint, rand);
74            }
75        }
76    }
77
78private:
79    typedef GM INHERITED;
80};
81
82class Strokes2GM : public GM {
83    SkPath fPath;
84public:
85    Strokes2GM() {
86        SkRandom rand;
87        fPath.moveTo(0, 0);
88        for (int i = 0; i < 13; i++) {
89            SkScalar x = rand.nextUScalar1() * (W >> 1);
90            SkScalar y = rand.nextUScalar1() * (H >> 1);
91            fPath.lineTo(x, y);
92        }
93    }
94
95protected:
96    virtual SkString onShortName() {
97        return SkString("strokes_poly");
98    }
99
100    virtual SkISize onISize() {
101        return make_isize(W, H*2);
102    }
103
104    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
110    virtual void onDraw(SkCanvas* canvas) {
111        canvas->drawColor(SK_ColorWHITE);
112
113        SkPaint paint;
114        paint.setStyle(SkPaint::kStroke_Style);
115        paint.setStrokeWidth(SkIntToScalar(9)/2);
116
117        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)));
125
126            SkRandom rand;
127            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    }
135
136private:
137    typedef GM INHERITED;
138};
139
140//////////////////////////////////////////////////////////////////////////////
141
142static GM* MyFactory(void*) { return new StrokesGM; }
143static GMRegistry reg(MyFactory);
144
145static GM* MyFactory2(void*) { return new Strokes2GM; }
146static GMRegistry reg2(MyFactory2);
147
148}
149
150