Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ovals.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 Google 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
8#include "gm/gm.h"
14#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
23#include "src/base/SkRandom.h"
24#include "tools/ToolUtils.h"
25
26using namespace skia_private;
27
28namespace skiagm {
29
30class OvalGM : public GM {
31public:
33 this->setBGColor(0xFF000000);
34 this->makePaints();
35 this->makeMatrices();
36 }
37
38protected:
39 SkString getName() const override { return SkString("ovals"); }
40
41 SkISize getISize() override { return SkISize::Make(1200, 900); }
42
43 void makePaints() {
44 {
45 // no AA
46 SkPaint p;
47 fPaints.push_back(p);
48 }
49
50 {
51 // AA
52 SkPaint p;
53 p.setAntiAlias(true);
54 fPaints.push_back(p);
55 }
56
57 {
58 // AA with stroke style
59 SkPaint p;
60 p.setAntiAlias(true);
61 p.setStyle(SkPaint::kStroke_Style);
62 p.setStrokeWidth(SkIntToScalar(5));
63 fPaints.push_back(p);
64 }
65
66 {
67 // AA with stroke style, width = 0
68 SkPaint p;
69 p.setAntiAlias(true);
70 p.setStyle(SkPaint::kStroke_Style);
71 fPaints.push_back(p);
72 }
73
74 {
75 // AA with stroke and fill style
76 SkPaint p;
77 p.setAntiAlias(true);
79 p.setStrokeWidth(SkIntToScalar(3));
80 fPaints.push_back(p);
81 }
82 }
83
84 void makeMatrices() {
85 {
86 SkMatrix m;
87 m.setIdentity();
88 fMatrices.push_back(m);
89 }
90
91 {
92 SkMatrix m;
93 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
94 fMatrices.push_back(m);
95 }
96
97 {
98 SkMatrix m;
99 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
100 fMatrices.push_back(m);
101 }
102
103 {
104 SkMatrix m;
105 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
106 fMatrices.push_back(m);
107 }
108
109 {
110 SkMatrix m;
111 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
112 fMatrices.push_back(m);
113 }
114
115 {
116 SkMatrix m;
117 m.setRotate(SkIntToScalar(90));
118 fMatrices.push_back(m);
119 }
120
121 {
122 SkMatrix m;
123 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
124 fMatrices.push_back(m);
125 }
126
127 {
128 SkMatrix m;
129 m.setRotate(SkIntToScalar(60));
130 fMatrices.push_back(m);
131 }
132 }
133
135 SkScalar hsv[3];
136 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
137 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
138 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
139
141 }
142
143 void onDraw(SkCanvas* canvas) override {
144 SkRandom rand(1);
145 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
146 const SkRect kOval = SkRect::MakeLTRB(-20, -30, 20, 30);
147
148 const SkScalar kXStart = 60.0f;
149 const SkScalar kYStart = 80.0f;
150 const int kXStep = 150;
151 const int kYStep = 160;
152 int maxX = fMatrices.size();
153
154 SkPaint rectPaint;
155 rectPaint.setAntiAlias(true);
157 rectPaint.setStrokeWidth(SkIntToScalar(0));
158 rectPaint.setColor(SK_ColorLTGRAY);
159
160 int testCount = 0;
161 for (int i = 0; i < fPaints.size(); ++i) {
162 for (int j = 0; j < fMatrices.size(); ++j) {
163 canvas->save();
164 SkMatrix mat = fMatrices[j];
165 // position the oval, and make it at off-integer coords.
166 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
167 SK_Scalar1 / 4,
168 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
169 3 * SK_Scalar1 / 4);
170 canvas->concat(mat);
171
172 SkColor color = genColor(&rand);
173 fPaints[i].setColor(color);
174
175 canvas->drawRect(kOval, rectPaint);
176 canvas->drawOval(kOval, fPaints[i]);
177
178 canvas->restore();
179
180 ++testCount;
181 }
182 }
183
184 // special cases
185
186 // non-scaled tall and skinny oval
187 for (int i = 0; i < fPaints.size(); ++i) {
188 SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
189 canvas->save();
190 // position the oval, and make it at off-integer coords.
191 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
192 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
193
194 SkColor color = genColor(&rand);
195 fPaints[i].setColor(color);
196
197 canvas->drawRect(oval, rectPaint);
198 canvas->drawOval(oval, fPaints[i]);
199 canvas->restore();
200 }
201
202 // non-scaled wide and short oval
203 for (int i = 0; i < fPaints.size(); ++i) {
204 SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
205 canvas->save();
206 // position the oval, and make it at off-integer coords.
207 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
208 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
209 SK_ScalarHalf * kYStep);
210
211 SkColor color = genColor(&rand);
212 fPaints[i].setColor(color);
213
214 canvas->drawRect(oval, rectPaint);
215 canvas->drawOval(oval, fPaints[i]);
216 canvas->restore();
217 }
218
219 // super skinny oval
220 for (int i = 0; i < fPaints.size(); ++i) {
221 SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
222 canvas->save();
223 // position the oval, and make it at off-integer coords.
224 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
225 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
226
227 SkColor color = genColor(&rand);
228 fPaints[i].setColor(color);
229
230 canvas->drawOval(oval, fPaints[i]);
231 canvas->restore();
232 }
233
234 // super short oval
235 for (int i = 0; i < fPaints.size(); ++i) {
236 SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
237 canvas->save();
238 // position the oval, and make it at off-integer coords.
239 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
240 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
241 SK_ScalarHalf * kYStep);
242
243 SkColor color = genColor(&rand);
244 fPaints[i].setColor(color);
245
246 canvas->drawOval(oval, fPaints[i]);
247 canvas->restore();
248 }
249
250 // radial gradient
254 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, std::size(colors),
256
257 for (int i = 0; i < fPaints.size(); ++i) {
258 canvas->save();
259 // position the path, and make it at off-integer coords.
260 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
261 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
262 SK_ScalarHalf * kYStep);
263
264 SkColor color = genColor(&rand);
265 fPaints[i].setColor(color);
266 fPaints[i].setShader(shader);
267
268 canvas->drawRect(kOval, rectPaint);
269 canvas->drawOval(kOval, fPaints[i]);
270
271 fPaints[i].setShader(nullptr);
272
273 canvas->restore();
274 }
275
276 // reflected oval
277 for (int i = 0; i < fPaints.size(); ++i) {
278 SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30);
279 canvas->save();
280 // position the oval, and make it at off-integer coords.
281 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
282 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
283 SK_ScalarHalf * kYStep);
284 canvas->rotate(90);
285 canvas->scale(1, -1);
286 canvas->scale(1, 0.66f);
287
288 SkColor color = genColor(&rand);
289 fPaints[i].setColor(color);
290
291 canvas->drawRect(oval, rectPaint);
292 canvas->drawOval(oval, fPaints[i]);
293 canvas->restore();
294 }
295 }
296
297private:
298 TArray<SkPaint> fPaints;
299 TArray<SkMatrix> fMatrices;
300
301 using INHERITED = GM;
302};
303
304//////////////////////////////////////////////////////////////////////////////
305
306DEF_GM( return new OvalGM; )
307
308} // namespace skiagm
SkPoint pos
SkColor4f color
static const size_t testCount
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
uint32_t SkColor
Definition SkColor.h:37
SK_API SkColor SkHSVToColor(U8CPU alpha, const SkScalar hsv[3])
Definition SkColor.cpp:78
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
#define SK_Scalar1
Definition SkScalar.h:18
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkIntToScalar(x)
Definition SkScalar.h:57
static SkScalar center(float pos0, float pos1)
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawOval(const SkRect &oval, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void rotate(SkScalar degrees)
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
void concat(const SkMatrix &matrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:281
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition SkPaint.h:195
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
float nextRangeF(float min, float max)
Definition SkRandom.h:64
int size() const
Definition SkTArray.h:416
void setBGColor(SkColor)
Definition gm.cpp:159
SkColor genColor(SkRandom *rand)
Definition ovals.cpp:134
SkString getName() const override
Definition ovals.cpp:39
void makePaints()
Definition ovals.cpp:43
SkISize getISize() override
Definition ovals.cpp:41
void onDraw(SkCanvas *canvas) override
Definition ovals.cpp:143
void makeMatrices()
Definition ovals.cpp:84
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
SkColor color_to_565(SkColor color)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkPoint Make(float x, float y)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646