Flutter Engine
The Flutter Engine
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"
13#include "include/core/SkPath.h"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
25#include "src/base/SkRandom.h"
26#include "tools/ToolUtils.h"
27
28using namespace skia_private;
29
30namespace skiagm {
31
32class OvalGM : public GM {
33public:
35 this->setBGColor(0xFF000000);
36 this->makePaints();
37 this->makeMatrices();
38 }
39
40protected:
41 SkString getName() const override { return SkString("ovals"); }
42
43 SkISize getISize() override { return SkISize::Make(1200, 900); }
44
45 void makePaints() {
46 {
47 // no AA
48 SkPaint p;
49 fPaints.push_back(p);
50 }
51
52 {
53 // AA
54 SkPaint p;
55 p.setAntiAlias(true);
56 fPaints.push_back(p);
57 }
58
59 {
60 // AA with stroke style
61 SkPaint p;
62 p.setAntiAlias(true);
63 p.setStyle(SkPaint::kStroke_Style);
64 p.setStrokeWidth(SkIntToScalar(5));
65 fPaints.push_back(p);
66 }
67
68 {
69 // AA with stroke style, width = 0
70 SkPaint p;
71 p.setAntiAlias(true);
72 p.setStyle(SkPaint::kStroke_Style);
73 fPaints.push_back(p);
74 }
75
76 {
77 // AA with stroke and fill style
78 SkPaint p;
79 p.setAntiAlias(true);
81 p.setStrokeWidth(SkIntToScalar(3));
82 fPaints.push_back(p);
83 }
84 }
85
86 void makeMatrices() {
87 {
88 SkMatrix m;
89 m.setIdentity();
90 fMatrices.push_back(m);
91 }
92
93 {
94 SkMatrix m;
95 m.setScale(SkIntToScalar(3), SkIntToScalar(2));
96 fMatrices.push_back(m);
97 }
98
99 {
100 SkMatrix m;
101 m.setScale(SkIntToScalar(2), SkIntToScalar(2));
102 fMatrices.push_back(m);
103 }
104
105 {
106 SkMatrix m;
107 m.setScale(SkIntToScalar(1), SkIntToScalar(2));
108 fMatrices.push_back(m);
109 }
110
111 {
112 SkMatrix m;
113 m.setScale(SkIntToScalar(4), SkIntToScalar(1));
114 fMatrices.push_back(m);
115 }
116
117 {
118 SkMatrix m;
119 m.setRotate(SkIntToScalar(90));
120 fMatrices.push_back(m);
121 }
122
123 {
124 SkMatrix m;
125 m.setSkew(SkIntToScalar(2), SkIntToScalar(3));
126 fMatrices.push_back(m);
127 }
128
129 {
130 SkMatrix m;
131 m.setRotate(SkIntToScalar(60));
132 fMatrices.push_back(m);
133 }
134 }
135
137 SkScalar hsv[3];
138 hsv[0] = rand->nextRangeF(0.0f, 360.0f);
139 hsv[1] = rand->nextRangeF(0.75f, 1.0f);
140 hsv[2] = rand->nextRangeF(0.75f, 1.0f);
141
143 }
144
145 void onDraw(SkCanvas* canvas) override {
146 SkRandom rand(1);
147 canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
148 const SkRect kOval = SkRect::MakeLTRB(-20, -30, 20, 30);
149
150 const SkScalar kXStart = 60.0f;
151 const SkScalar kYStart = 80.0f;
152 const int kXStep = 150;
153 const int kYStep = 160;
154 int maxX = fMatrices.size();
155
156 SkPaint rectPaint;
157 rectPaint.setAntiAlias(true);
159 rectPaint.setStrokeWidth(SkIntToScalar(0));
160 rectPaint.setColor(SK_ColorLTGRAY);
161
162 int testCount = 0;
163 for (int i = 0; i < fPaints.size(); ++i) {
164 for (int j = 0; j < fMatrices.size(); ++j) {
165 canvas->save();
166 SkMatrix mat = fMatrices[j];
167 // position the oval, and make it at off-integer coords.
168 mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) +
169 SK_Scalar1 / 4,
170 kYStart + SK_Scalar1 * kYStep * (testCount / maxX) +
171 3 * SK_Scalar1 / 4);
172 canvas->concat(mat);
173
174 SkColor color = genColor(&rand);
175 fPaints[i].setColor(color);
176
177 canvas->drawRect(kOval, rectPaint);
178 canvas->drawOval(kOval, fPaints[i]);
179
180 canvas->restore();
181
182 ++testCount;
183 }
184 }
185
186 // special cases
187
188 // non-scaled tall and skinny oval
189 for (int i = 0; i < fPaints.size(); ++i) {
190 SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60);
191 canvas->save();
192 // position the oval, and make it at off-integer coords.
193 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4,
194 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
195
196 SkColor color = genColor(&rand);
197 fPaints[i].setColor(color);
198
199 canvas->drawRect(oval, rectPaint);
200 canvas->drawOval(oval, fPaints[i]);
201 canvas->restore();
202 }
203
204 // non-scaled wide and short oval
205 for (int i = 0; i < fPaints.size(); ++i) {
206 SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30);
207 canvas->save();
208 // position the oval, and make it at off-integer coords.
209 canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4,
210 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
211 SK_ScalarHalf * kYStep);
212
213 SkColor color = genColor(&rand);
214 fPaints[i].setColor(color);
215
216 canvas->drawRect(oval, rectPaint);
217 canvas->drawOval(oval, fPaints[i]);
218 canvas->restore();
219 }
220
221 // super skinny oval
222 for (int i = 0; i < fPaints.size(); ++i) {
223 SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60);
224 canvas->save();
225 // position the oval, and make it at off-integer coords.
226 canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4,
227 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4);
228
229 SkColor color = genColor(&rand);
230 fPaints[i].setColor(color);
231
232 canvas->drawOval(oval, fPaints[i]);
233 canvas->restore();
234 }
235
236 // super short oval
237 for (int i = 0; i < fPaints.size(); ++i) {
238 SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0);
239 canvas->save();
240 // position the oval, and make it at off-integer coords.
241 canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4,
242 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
243 SK_ScalarHalf * kYStep);
244
245 SkColor color = genColor(&rand);
246 fPaints[i].setColor(color);
247
248 canvas->drawOval(oval, fPaints[i]);
249 canvas->restore();
250 }
251
252 // radial gradient
256 auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, std::size(colors),
258
259 for (int i = 0; i < fPaints.size(); ++i) {
260 canvas->save();
261 // position the path, and make it at off-integer coords.
262 canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4,
263 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
264 SK_ScalarHalf * kYStep);
265
266 SkColor color = genColor(&rand);
267 fPaints[i].setColor(color);
268 fPaints[i].setShader(shader);
269
270 canvas->drawRect(kOval, rectPaint);
271 canvas->drawOval(kOval, fPaints[i]);
272
273 fPaints[i].setShader(nullptr);
274
275 canvas->restore();
276 }
277
278 // reflected oval
279 for (int i = 0; i < fPaints.size(); ++i) {
280 SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30);
281 canvas->save();
282 // position the oval, and make it at off-integer coords.
283 canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4,
284 kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 +
285 SK_ScalarHalf * kYStep);
286 canvas->rotate(90);
287 canvas->scale(1, -1);
288 canvas->scale(1, 0.66f);
289
290 SkColor color = genColor(&rand);
291 fPaints[i].setColor(color);
292
293 canvas->drawRect(oval, rectPaint);
294 canvas->drawOval(oval, fPaints[i]);
295 canvas->restore();
296 }
297 }
298
299private:
300 TArray<SkPaint> fPaints;
301 TArray<SkMatrix> fMatrices;
302
303 using INHERITED = GM;
304};
305
306//////////////////////////////////////////////////////////////////////////////
307
308DEF_GM( return new OvalGM; )
309
310} // namespace skiagm
311
312DEF_SIMPLE_GM(open_ovals, canvas, 225, 110) {
314 paint.setAntiAlias(true);
316
317 const SkRect rect = SkRect::MakeWH(50, 100);
319 const int start = 1;
320
321 // We stroke several open ovals to see how they behave
322 canvas->translate(5, 5);
323
324 auto doRow = [&](const SkPath& p) {
325 canvas->drawPath(p, paint);
326 canvas->translate(55, 0);
327 };
328
329 // Default case (left open) looks like an oval
330 SkPath path;
331 path.addOpenOval(rect, dir, start);
332 doRow(path);
333
334 // Closing makes us technically be an oval, but should look the same
335 path.close();
336 doRow(path);
337
338 // Moving before the oval adds a line to the start
339 path.reset();
340 path.moveTo(rect.center());
341 path.addOpenOval(rect, dir, start);
342 doRow(path);
343
344 // Similarly, lineTo after the oval starts from the start/end point
345 path.reset();
346 path.addOpenOval(rect, dir, start);
347 path.lineTo(rect.center());
348 doRow(path);
349}
SkPoint pos
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
SkPathDirection
Definition: SkPathTypes.h:34
#define SK_Scalar1
Definition: SkScalar.h:18
#define SK_ScalarHalf
Definition: SkScalar.h:19
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
int save()
Definition: SkCanvas.cpp:447
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
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
Definition: SkPath.h:59
float nextRangeF(float min, float max)
Definition: SkRandom.h:64
int size() const
Definition: SkTArray.h:421
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
SkColor genColor(SkRandom *rand)
Definition: ovals.cpp:136
SkString getName() const override
Definition: ovals.cpp:41
void makePaints()
Definition: ovals.cpp:45
SkISize getISize() override
Definition: ovals.cpp:43
void onDraw(SkCanvas *canvas) override
Definition: ovals.cpp:145
void makeMatrices()
Definition: ovals.cpp:86
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
SkRect oval
Definition: SkRecords.h:249
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets dir
Definition: switches.h:145
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DEF_SIMPLE_GM(open_ovals, canvas, 225, 110)
Definition: ovals.cpp:312
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646