1/*
2 * Copyright 2012 Intel Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "gm.h"
8#include "SkBlurDrawLooper.h"
9#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
11#include "SkColorFilter.h"
12#include "SkGradientShader.h"
13#include "SkMatrix.h"
14#include "SkRandom.h"
15#include "SkTArray.h"
16
17namespace skiagm {
18
19class CircleGM : public GM {
20public:
21    CircleGM() {
22        this->setBGColor(0xFF000000);
23        this->makePaints();
24        this->makeMatrices();
25    }
26
27protected:
28
29    SkString onShortName() override {
30        return SkString("circles");
31    }
32
33    SkISize onISize() override {
34        return SkISize::Make(1200, 900);
35    }
36
37    void makePaints() {
38        {
39        // no AA
40        SkPaint p;
41        fPaints.push_back(p);
42        }
43
44        {
45        // AA
46        SkPaint p;
47        p.setAntiAlias(true);
48        fPaints.push_back(p);
49        }
50
51        {
52        // AA with mask filter
53        SkPaint p;
54        p.setAntiAlias(true);
55        p.setMaskFilter(SkBlurMaskFilter::Make(
56                               kNormal_SkBlurStyle,
57                               SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
58                               SkBlurMaskFilter::kHighQuality_BlurFlag));
59        fPaints.push_back(p);
60        }
61
62        {
63        // AA with radial shader
64        SkPaint p;
65        p.setAntiAlias(true);
66        SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
67        SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
68        SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
69        p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors),
70                                                 SkShader::kClamp_TileMode));
71        fPaints.push_back(p);
72        }
73
74        {
75        // AA with blur
76        SkPaint p;
77        p.setAntiAlias(true);
78        p.setLooper(SkBlurDrawLooper::Make(SK_ColorBLUE,
79                                     SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
80                                     SkIntToScalar(5), SkIntToScalar(10)));
81        fPaints.push_back(p);
82        }
83
84        {
85        // AA with stroke style
86        SkPaint p;
87        p.setAntiAlias(true);
88        p.setStyle(SkPaint::kStroke_Style);
89        p.setStrokeWidth(SkIntToScalar(3));
90        fPaints.push_back(p);
91        }
92
93        {
94        // AA with stroke style, width = 0
95        SkPaint p;
96        p.setAntiAlias(true);
97        p.setStyle(SkPaint::kStroke_Style);
98        fPaints.push_back(p);
99        }
100
101        {
102        // AA with stroke and fill style
103        SkPaint p;
104        p.setAntiAlias(true);
105        p.setStyle(SkPaint::kStrokeAndFill_Style);
106        p.setStrokeWidth(SkIntToScalar(2));
107        fPaints.push_back(p);
108        }
109    }
110
111    void makeMatrices() {
112        {
113        SkMatrix m;
114        m.setScale(SkIntToScalar(2), SkIntToScalar(3));
115        fMatrices.push_back(m);
116        }
117
118        {
119        SkMatrix m;
120        m.setScale(SkIntToScalar(2), SkIntToScalar(2));
121        fMatrices.push_back(m);
122        }
123
124        {
125        SkMatrix m;
126        m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
127        fMatrices.push_back(m);
128        }
129
130        {
131        SkMatrix m;
132        m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
133        fMatrices.push_back(m);
134        }
135
136        {
137        SkMatrix m;
138        m.setRotate(SkIntToScalar(30));
139        fMatrices.push_back(m);
140        }
141    }
142
143    void onDraw(SkCanvas* canvas) override {
144        // Draw a giant AA circle as the background.
145        SkISize size = this->getISize();
146        SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
147                                      SkIntToScalar(size.fHeight)) / 2.f;
148        SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
149                                            SkIntToScalar(size.fHeight/2));
150        SkPaint giantPaint;
151        giantPaint.setAntiAlias(true);
152        giantPaint.setColor(0x80808080);
153        canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint);
154
155        SkRandom rand;
156        canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
157        int i;
158        for (i = 0; i < fPaints.count(); ++i) {
159            canvas->save();
160            // position the path, and make it at off-integer coords.
161            canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
162                              SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
163            SkColor color = rand.nextU();
164            color |= 0xff000000;
165            fPaints[i].setColor(color);
166
167            canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
168                               SkIntToScalar(20),
169                               fPaints[i]);
170            canvas->restore();
171        }
172
173        for (int j = 0; j < fMatrices.count(); ++j, ++i) {
174            canvas->save();
175
176            canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
177                              SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
178
179            canvas->concat(fMatrices[j]);
180
181            SkPaint paint;
182            paint.setAntiAlias(true);
183
184            SkColor color = rand.nextU();
185            color |= 0xff000000;
186            paint.setColor(color);
187
188            canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
189                               SkIntToScalar(20),
190                               paint);
191
192            canvas->restore();
193        }
194    }
195
196private:
197    typedef GM INHERITED;
198    SkTArray<SkPaint> fPaints;
199    SkTArray<SkMatrix> fMatrices;
200};
201
202//////////////////////////////////////////////////////////////////////////////
203
204static GM* MyFactory(void*) { return new CircleGM; }
205static GMRegistry reg(MyFactory);
206
207}
208