1
2/*
3 * Copyright 2012 Intel 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#include "gm.h"
9#include "SkBlurDrawLooper.h"
10#include "SkBlurMask.h"
11#include "SkBlurMaskFilter.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    virtual uint32_t onGetFlags() const SK_OVERRIDE {
29        return kSkipTiled_Flag;
30    }
31
32    virtual SkString onShortName() SK_OVERRIDE {
33        return SkString("circles");
34    }
35
36    virtual SkISize onISize() SK_OVERRIDE {
37        return SkISize::Make(1200, 900);
38    }
39
40    void makePaints() {
41        {
42        // no AA
43        SkPaint p;
44        fPaints.push_back(p);
45        }
46
47        {
48        // AA
49        SkPaint p;
50        p.setAntiAlias(true);
51        fPaints.push_back(p);
52        }
53
54        {
55        // AA with mask filter
56        SkPaint p;
57        p.setAntiAlias(true);
58        SkMaskFilter* mf = SkBlurMaskFilter::Create(
59                               kNormal_SkBlurStyle,
60                               SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
61                               SkBlurMaskFilter::kHighQuality_BlurFlag);
62        p.setMaskFilter(mf)->unref();
63        fPaints.push_back(p);
64        }
65
66        {
67        // AA with radial shader
68        SkPaint p;
69        p.setAntiAlias(true);
70        SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
71        SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
72        SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
73        SkShader* s = SkGradientShader::CreateRadial(center,
74                                                     SkIntToScalar(20),
75                                                     colors,
76                                                     pos,
77                                                     SK_ARRAY_COUNT(colors),
78                                                     SkShader::kClamp_TileMode);
79        p.setShader(s)->unref();
80        fPaints.push_back(p);
81        }
82
83        {
84        // AA with blur
85        SkPaint p;
86        p.setAntiAlias(true);
87        SkBlurDrawLooper* shadowLooper =
88            SkBlurDrawLooper::Create(SK_ColorBLUE,
89                                     SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
90                                     SkIntToScalar(5), SkIntToScalar(10),
91                                     SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
92                                     SkBlurDrawLooper::kOverrideColor_BlurFlag |
93                                     SkBlurDrawLooper::kHighQuality_BlurFlag);
94        SkAutoUnref aurL0(shadowLooper);
95        p.setLooper(shadowLooper);
96        fPaints.push_back(p);
97        }
98
99        {
100        // AA with stroke style
101        SkPaint p;
102        p.setAntiAlias(true);
103        p.setStyle(SkPaint::kStroke_Style);
104        p.setStrokeWidth(SkIntToScalar(3));
105        fPaints.push_back(p);
106        }
107
108        {
109        // AA with stroke style, width = 0
110        SkPaint p;
111        p.setAntiAlias(true);
112        p.setStyle(SkPaint::kStroke_Style);
113        fPaints.push_back(p);
114        }
115
116        {
117        // AA with stroke and fill style
118        SkPaint p;
119        p.setAntiAlias(true);
120        p.setStyle(SkPaint::kStrokeAndFill_Style);
121        p.setStrokeWidth(SkIntToScalar(2));
122        fPaints.push_back(p);
123        }
124    }
125
126    void makeMatrices() {
127        {
128        SkMatrix m;
129        m.setScale(SkIntToScalar(2), SkIntToScalar(3));
130        fMatrices.push_back(m);
131        }
132
133        {
134        SkMatrix m;
135        m.setScale(SkIntToScalar(2), SkIntToScalar(2));
136        fMatrices.push_back(m);
137        }
138
139        {
140        SkMatrix m;
141        m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
142        fMatrices.push_back(m);
143        }
144
145        {
146        SkMatrix m;
147        m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
148        fMatrices.push_back(m);
149        }
150
151        {
152        SkMatrix m;
153        m.setRotate(SkIntToScalar(30));
154        fMatrices.push_back(m);
155        }
156    }
157
158    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
159        SkLCGRandom rand;
160        canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
161        int i;
162        for (i = 0; i < fPaints.count(); ++i) {
163            canvas->save();
164            // position the path, and make it at off-integer coords.
165            canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
166                              SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
167            SkColor color = rand.nextU();
168            color |= 0xff000000;
169            fPaints[i].setColor(color);
170
171            canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
172                               SkIntToScalar(20),
173                               fPaints[i]);
174            canvas->restore();
175        }
176
177        for (int j = 0; j < fMatrices.count(); ++j, ++i) {
178            canvas->save();
179
180            canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
181                              SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
182
183            canvas->concat(fMatrices[j]);
184
185            SkPaint paint;
186            paint.setAntiAlias(true);
187
188            SkColor color = rand.nextU();
189            color |= 0xff000000;
190            paint.setColor(color);
191
192            canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
193                               SkIntToScalar(20),
194                               paint);
195
196            canvas->restore();
197        }
198    }
199
200private:
201    typedef GM INHERITED;
202    SkTArray<SkPaint> fPaints;
203    SkTArray<SkMatrix> fMatrices;
204};
205
206//////////////////////////////////////////////////////////////////////////////
207
208static GM* MyFactory(void*) { return new CircleGM; }
209static GMRegistry reg(MyFactory);
210
211}
212