c0503fdc1dd6b14af04cd2a5a7a694dfe7c31b0f
[platform/upstream/libSkiaSharp.git] / gm / circles.cpp
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
17 namespace skiagm {
18
19 class CircleGM : public GM {
20 public:
21     CircleGM() {
22         this->setBGColor(0xFF000000);
23         this->makePaints();
24         this->makeMatrices();
25     }
26
27 protected:
28
29     SkString onShortName() SK_OVERRIDE {
30         return SkString("circles");
31     }
32
33     SkISize onISize() SK_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         SkMaskFilter* mf = SkBlurMaskFilter::Create(
56                                kNormal_SkBlurStyle,
57                                SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
58                                SkBlurMaskFilter::kHighQuality_BlurFlag);
59         p.setMaskFilter(mf)->unref();
60         fPaints.push_back(p);
61         }
62
63         {
64         // AA with radial shader
65         SkPaint p;
66         p.setAntiAlias(true);
67         SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40));
68         SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN };
69         SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 };
70         SkShader* s = SkGradientShader::CreateRadial(center,
71                                                      SkIntToScalar(20),
72                                                      colors,
73                                                      pos,
74                                                      SK_ARRAY_COUNT(colors),
75                                                      SkShader::kClamp_TileMode);
76         p.setShader(s)->unref();
77         fPaints.push_back(p);
78         }
79
80         {
81         // AA with blur
82         SkPaint p;
83         p.setAntiAlias(true);
84         SkBlurDrawLooper* shadowLooper =
85             SkBlurDrawLooper::Create(SK_ColorBLUE,
86                                      SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)),
87                                      SkIntToScalar(5), SkIntToScalar(10),
88                                      SkBlurDrawLooper::kIgnoreTransform_BlurFlag |
89                                      SkBlurDrawLooper::kOverrideColor_BlurFlag |
90                                      SkBlurDrawLooper::kHighQuality_BlurFlag);
91         SkAutoUnref aurL0(shadowLooper);
92         p.setLooper(shadowLooper);
93         fPaints.push_back(p);
94         }
95
96         {
97         // AA with stroke style
98         SkPaint p;
99         p.setAntiAlias(true);
100         p.setStyle(SkPaint::kStroke_Style);
101         p.setStrokeWidth(SkIntToScalar(3));
102         fPaints.push_back(p);
103         }
104
105         {
106         // AA with stroke style, width = 0
107         SkPaint p;
108         p.setAntiAlias(true);
109         p.setStyle(SkPaint::kStroke_Style);
110         fPaints.push_back(p);
111         }
112
113         {
114         // AA with stroke and fill style
115         SkPaint p;
116         p.setAntiAlias(true);
117         p.setStyle(SkPaint::kStrokeAndFill_Style);
118         p.setStrokeWidth(SkIntToScalar(2));
119         fPaints.push_back(p);
120         }
121     }
122
123     void makeMatrices() {
124         {
125         SkMatrix m;
126         m.setScale(SkIntToScalar(2), SkIntToScalar(3));
127         fMatrices.push_back(m);
128         }
129
130         {
131         SkMatrix m;
132         m.setScale(SkIntToScalar(2), SkIntToScalar(2));
133         fMatrices.push_back(m);
134         }
135
136         {
137         SkMatrix m;
138         m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
139         fMatrices.push_back(m);
140         }
141
142         {
143         SkMatrix m;
144         m.setSkew(SkIntToScalar(2), SkIntToScalar(2));
145         fMatrices.push_back(m);
146         }
147
148         {
149         SkMatrix m;
150         m.setRotate(SkIntToScalar(30));
151         fMatrices.push_back(m);
152         }
153     }
154
155     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
156         // Draw a giant AA circle as the background.
157         SkISize size = this->getISize();
158         SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth),
159                                       SkIntToScalar(size.fHeight)) / 2.f;
160         SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2),
161                                             SkIntToScalar(size.fHeight/2));
162         SkPaint giantPaint;
163         giantPaint.setAntiAlias(true);
164         giantPaint.setColor(0x80808080);
165         canvas->drawCircle(giantCenter.fX, giantCenter.fY, giantRadius, giantPaint);
166         
167         SkRandom rand;
168         canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
169         int i;
170         for (i = 0; i < fPaints.count(); ++i) {
171             canvas->save();
172             // position the path, and make it at off-integer coords.
173             canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
174                               SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
175             SkColor color = rand.nextU();
176             color |= 0xff000000;
177             fPaints[i].setColor(color);
178
179             canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
180                                SkIntToScalar(20),
181                                fPaints[i]);
182             canvas->restore();
183         }
184
185         for (int j = 0; j < fMatrices.count(); ++j, ++i) {
186             canvas->save();
187
188             canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4,
189                               SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4);
190
191             canvas->concat(fMatrices[j]);
192
193             SkPaint paint;
194             paint.setAntiAlias(true);
195
196             SkColor color = rand.nextU();
197             color |= 0xff000000;
198             paint.setColor(color);
199
200             canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40),
201                                SkIntToScalar(20),
202                                paint);
203
204             canvas->restore();
205         }
206     }
207
208 private:
209     typedef GM INHERITED;
210     SkTArray<SkPaint> fPaints;
211     SkTArray<SkMatrix> fMatrices;
212 };
213
214 //////////////////////////////////////////////////////////////////////////////
215
216 static GM* MyFactory(void*) { return new CircleGM; }
217 static GMRegistry reg(MyFactory);
218
219 }