Flutter Engine
The Flutter Engine
degeneratesegments.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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"
11#include "include/core/SkFont.h"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
21#include "src/base/SkRandom.h"
22#include "tools/ToolUtils.h"
24
25namespace skiagm {
26
27class DegenerateSegmentsGM : public GM {
28 struct PathAndName {
30 const char* fName1;
31 const char* fName2;
32 };
33
34 SkString getName() const override { return SkString("degeneratesegments"); }
35
36 SkISize getISize() override { return {896, 930}; }
37
38 typedef SkPoint (*AddSegmentFunc)(SkPathBuilder&, SkPoint&);
39
40 // We need to use explicit commands here, instead of addPath, because we
41 // do not want the moveTo that is added at the beginning of a path to
42 // appear in the appended path.
43 static SkPoint AddMove(SkPathBuilder& path, SkPoint& startPt) {
44 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
45 path.moveTo(moveToPt);
46 return moveToPt;
47 }
48
49 static SkPoint AddMoveClose(SkPathBuilder& path, SkPoint& startPt) {
50 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
51 path.moveTo(moveToPt);
52 path.close();
53 return moveToPt;
54 }
55
56 static SkPoint AddDegenLine(SkPathBuilder& path, SkPoint& startPt) {
57 path.lineTo(startPt);
58 return startPt;
59 }
60
61 static SkPoint AddMoveDegenLine(SkPathBuilder& path, SkPoint& startPt) {
62 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
63 path.moveTo(moveToPt);
64 path.lineTo(moveToPt);
65 return moveToPt;
66 }
67
68 static SkPoint AddMoveDegenLineClose(SkPathBuilder& path, SkPoint& startPt) {
69 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
70 path.moveTo(moveToPt);
71 path.lineTo(moveToPt);
72 path.close();
73 return moveToPt;
74 }
75
76 static SkPoint AddDegenQuad(SkPathBuilder& path, SkPoint& startPt) {
77 path.quadTo(startPt, startPt);
78 return startPt;
79 }
80
81 static SkPoint AddMoveDegenQuad(SkPathBuilder& path, SkPoint& startPt) {
82 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
83 path.moveTo(moveToPt);
84 path.quadTo(moveToPt, moveToPt);
85 return moveToPt;
86 }
87
88 static SkPoint AddMoveDegenQuadClose(SkPathBuilder& path, SkPoint& startPt) {
89 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
90 path.moveTo(moveToPt);
91 path.quadTo(moveToPt, moveToPt);
92 path.close();
93 return moveToPt;
94 }
95
96 static SkPoint AddDegenCubic(SkPathBuilder& path, SkPoint& startPt) {
97 path.cubicTo(startPt, startPt, startPt);
98 return startPt;
99 }
100
101 static SkPoint AddMoveDegenCubic(SkPathBuilder& path, SkPoint& startPt) {
102 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
103 path.moveTo(moveToPt);
104 path.cubicTo(moveToPt, moveToPt, moveToPt);
105 return moveToPt;
106 }
107
108 static SkPoint AddMoveDegenCubicClose(SkPathBuilder& path, SkPoint& startPt) {
109 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
110 path.moveTo(moveToPt);
111 path.cubicTo(moveToPt, moveToPt, moveToPt);
112 path.close();
113 return moveToPt;
114 }
115
116 static SkPoint AddClose(SkPathBuilder& path, SkPoint& startPt) {
117 path.close();
118 return startPt;
119 }
120
121 static SkPoint AddLine(SkPathBuilder& path, SkPoint& startPt) {
122 SkPoint endPt = startPt + SkPoint::Make(40*SK_Scalar1, 0);
123 path.lineTo(endPt);
124 return endPt;
125 }
126
127 static SkPoint AddMoveLine(SkPathBuilder& path, SkPoint& startPt) {
128 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
129 SkPoint endPt = moveToPt + SkPoint::Make(40*SK_Scalar1, 0);
130 path.moveTo(moveToPt);
131 path.lineTo(endPt);
132 return endPt;
133 }
134
135 static SkPoint AddMoveLineClose(SkPathBuilder& path, SkPoint& startPt) {
136 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
137 SkPoint endPt = moveToPt + SkPoint::Make(40*SK_Scalar1, 0);
138 path.moveTo(moveToPt);
139 path.lineTo(endPt);
140 path.close();
141 return endPt;
142 }
143
144 static SkPoint AddQuad(SkPathBuilder& path, SkPoint& startPt) {
145 SkPoint midPt = startPt + SkPoint::Make(20*SK_Scalar1, 5*SK_Scalar1);
146 SkPoint endPt = startPt + SkPoint::Make(40*SK_Scalar1, 0);
147 path.quadTo(midPt, endPt);
148 return endPt;
149 }
150
151 static SkPoint AddMoveQuad(SkPathBuilder& path, SkPoint& startPt) {
152 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
153 SkPoint midPt = moveToPt + SkPoint::Make(20*SK_Scalar1, 5*SK_Scalar1);
154 SkPoint endPt = moveToPt + SkPoint::Make(40*SK_Scalar1, 0);
155 path.moveTo(moveToPt);
156 path.quadTo(midPt, endPt);
157 return endPt;
158 }
159
160 static SkPoint AddMoveQuadClose(SkPathBuilder& path, SkPoint& startPt) {
161 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
162 SkPoint midPt = moveToPt + SkPoint::Make(20*SK_Scalar1, 5*SK_Scalar1);
163 SkPoint endPt = moveToPt + SkPoint::Make(40*SK_Scalar1, 0);
164 path.moveTo(moveToPt);
165 path.quadTo(midPt, endPt);
166 path.close();
167 return endPt;
168 }
169
170 static SkPoint AddCubic(SkPathBuilder& path, SkPoint& startPt) {
171 SkPoint t1Pt = startPt + SkPoint::Make(15*SK_Scalar1, 5*SK_Scalar1);
172 SkPoint t2Pt = startPt + SkPoint::Make(25*SK_Scalar1, 5*SK_Scalar1);
173 SkPoint endPt = startPt + SkPoint::Make(40*SK_Scalar1, 0);
174 path.cubicTo(t1Pt, t2Pt, endPt);
175 return endPt;
176 }
177
178 static SkPoint AddMoveCubic(SkPathBuilder& path, SkPoint& startPt) {
179 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
180 SkPoint t1Pt = moveToPt + SkPoint::Make(15*SK_Scalar1, 5*SK_Scalar1);
181 SkPoint t2Pt = moveToPt + SkPoint::Make(25*SK_Scalar1, 5*SK_Scalar1);
182 SkPoint endPt = moveToPt + SkPoint::Make(40*SK_Scalar1, 0);
183 path.moveTo(moveToPt);
184 path.cubicTo(t1Pt, t2Pt, endPt);
185 return endPt;
186 }
187
188 static SkPoint AddMoveCubicClose(SkPathBuilder& path, SkPoint& startPt) {
189 SkPoint moveToPt = startPt + SkPoint::Make(0, 10*SK_Scalar1);
190 SkPoint t1Pt = moveToPt + SkPoint::Make(15*SK_Scalar1, 5*SK_Scalar1);
191 SkPoint t2Pt = moveToPt + SkPoint::Make(25*SK_Scalar1, 5*SK_Scalar1);
192 SkPoint endPt = moveToPt + SkPoint::Make(40*SK_Scalar1, 0);
193 path.moveTo(moveToPt);
194 path.cubicTo(t1Pt, t2Pt, endPt);
195 path.close();
196 return endPt;
197 }
198
199 void drawPath(SkPath path, SkCanvas* canvas, SkColor color,
201 SkPaint::Style style, SkPathFillType fill,
203 path.setFillType(fill);
205 paint.setStrokeCap(cap);
206 paint.setStrokeWidth(strokeWidth);
207 paint.setStrokeJoin(join);
208 paint.setColor(color);
209 paint.setStyle(style);
210 canvas->save();
211 canvas->clipRect(clip);
212 canvas->drawPath(path, paint);
213 canvas->restore();
214 }
215
216 void onDraw(SkCanvas* canvas) override {
217 constexpr AddSegmentFunc gSegmentFunctions[] = {
218 AddMove,
219 AddMoveClose,
220 AddDegenLine,
221 AddMoveDegenLine,
222 AddMoveDegenLineClose,
223 AddDegenQuad,
224 AddMoveDegenQuad,
225 AddMoveDegenQuadClose,
226 AddDegenCubic,
227 AddMoveDegenCubic,
228 AddMoveDegenCubicClose,
229 AddClose,
230 AddLine,
231 AddMoveLine,
232 AddMoveLineClose,
233 AddQuad,
234 AddMoveQuad,
235 AddMoveQuadClose,
236 AddCubic,
237 AddMoveCubic,
238 AddMoveCubicClose
239 };
240 const char* gSegmentNames[] = {
241 "Move",
242 "MoveClose",
243 "DegenLine",
244 "MoveDegenLine",
245 "MoveDegenLineClose",
246 "DegenQuad",
247 "MoveDegenQuad",
248 "MoveDegenQuadClose",
249 "DegenCubic",
250 "MoveDegenCubic",
251 "MoveDegenCubicClose",
252 "Close",
253 "Line",
254 "MoveLine",
255 "MoveLineClose",
256 "Quad",
257 "MoveQuad",
258 "MoveQuadClose",
259 "Cubic",
260 "MoveCubic",
261 "MoveCubicClose"
262 };
263
264 struct FillAndName {
265 SkPathFillType fFill;
266 const char* fName;
267 };
268 constexpr FillAndName gFills[] = {
269 {SkPathFillType::kWinding, "Winding"},
270 {SkPathFillType::kEvenOdd, "Even / Odd"},
271 {SkPathFillType::kInverseWinding, "Inverse Winding"},
272 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"}
273 };
274 struct StyleAndName {
276 const char* fName;
277 };
278 constexpr StyleAndName gStyles[] = {
279 {SkPaint::kFill_Style, "Fill"},
280 {SkPaint::kStroke_Style, "Stroke 10"},
281 {SkPaint::kStrokeAndFill_Style, "Stroke 10 And Fill"}
282 };
283 struct CapAndName {
284 SkPaint::Cap fCap;
286 const char* fName;
287 };
288 constexpr CapAndName gCaps[] = {
292 };
293
294 SkPaint titlePaint;
295 titlePaint.setColor(SK_ColorBLACK);
296 titlePaint.setAntiAlias(true);
298 const char title[] = "Random Paths Drawn Into Rectangle Clips With "
299 "Indicated Style, Fill and Linecaps, "
300 "with Stroke width 6";
301 canvas->drawString(title, 20, 20, font, titlePaint);
302
303 SkRandom rand;
305 canvas->save();
306 canvas->translate(2*SK_Scalar1, 30 * SK_Scalar1); // The title
307 canvas->save();
308 unsigned numSegments = std::size(gSegmentFunctions);
309 unsigned numCaps = std::size(gCaps);
310 unsigned numStyles = std::size(gStyles);
311 unsigned numFills = std::size(gFills);
312 for (size_t row = 0; row < 6; ++row) {
313 if (0 < row) {
314 canvas->translate(0, rect.height() + 100*SK_Scalar1);
315 }
316 canvas->save();
317 for (size_t column = 0; column < 4; ++column) {
318 if (0 < column) {
319 canvas->translate(rect.width() + 4*SK_Scalar1, 0);
320 }
321
323 StyleAndName style = gStyles[(rand.nextU() >> 16) % numStyles];
324 CapAndName cap = gCaps[(rand.nextU() >> 16) % numCaps];
325 FillAndName fill = gFills[(rand.nextU() >> 16) % numFills];
326 unsigned s1 = (rand.nextU() >> 16) % numSegments;
327 unsigned s2 = (rand.nextU() >> 16) % numSegments;
328 unsigned s3 = (rand.nextU() >> 16) % numSegments;
329 unsigned s4 = (rand.nextU() >> 16) % numSegments;
330 unsigned s5 = (rand.nextU() >> 16) % numSegments;
331 SkPoint pt = SkPoint::Make(10*SK_Scalar1, 0);
333 pt = gSegmentFunctions[s1](path, pt);
334 pt = gSegmentFunctions[s2](path, pt);
335 pt = gSegmentFunctions[s3](path, pt);
336 pt = gSegmentFunctions[s4](path, pt);
337 pt = gSegmentFunctions[s5](path, pt);
338
339 this->drawPath(path.detach(), canvas, color, rect,
340 cap.fCap, cap.fJoin, style.fStyle,
341 fill.fFill, SK_Scalar1*6);
342
343 SkPaint rectPaint;
344 rectPaint.setColor(SK_ColorBLACK);
346 rectPaint.setStrokeWidth(-1);
347 rectPaint.setAntiAlias(true);
348 canvas->drawRect(rect, rectPaint);
349
350 SkPaint labelPaint;
351 labelPaint.setColor(color);
352 labelPaint.setAntiAlias(true);
353 font.setSize(10);
354 canvas->drawString(style.fName, 0, rect.height() + 12, font, labelPaint);
355 canvas->drawString(fill.fName, 0, rect.height() + 24, font, labelPaint);
356 canvas->drawString(cap.fName, 0, rect.height() + 36, font, labelPaint);
357 canvas->drawString(gSegmentNames[s1], 0, rect.height() + 48, font, labelPaint);
358 canvas->drawString(gSegmentNames[s2], 0, rect.height() + 60, font, labelPaint);
359 canvas->drawString(gSegmentNames[s3], 0, rect.height() + 72, font, labelPaint);
360 canvas->drawString(gSegmentNames[s4], 0, rect.height() + 84, font, labelPaint);
361 canvas->drawString(gSegmentNames[s5], 0, rect.height() + 96, font, labelPaint);
362 }
363 canvas->restore();
364 }
365 canvas->restore();
366 canvas->restore();
367 }
368};
369
370//////////////////////////////////////////////////////////////////////////////
371
372DEF_GM( return new DegenerateSegmentsGM; )
373
374} // namespace skiagm
SkPath fPath
SkPaint::Join fJoin
SkStrokeRec::Style fStyle
static const int strokeWidth
Definition: BlurTest.cpp:60
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
SkPathFillType
Definition: SkPathTypes.h:11
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define SK_Scalar1
Definition: SkScalar.h:18
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
Definition: SkFont.h:35
@ kRound_Cap
adds circle
Definition: SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition: SkPaint.h:334
@ kSquare_Cap
adds square
Definition: SkPaint.h:336
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
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition: SkPaint.h:195
@ kRound_Join
adds circle
Definition: SkPaint.h:360
@ kBevel_Join
connects outside edges
Definition: SkPaint.h:361
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
Definition: SkPath.h:59
uint32_t nextU()
Definition: SkRandom.h:42
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
sk_sp< SkTypeface > DefaultPortableTypeface()
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
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
font
Font Metadata and Metrics.
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
const char * fName
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741
Definition: SkSize.h:16
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 SkFontStyle gStyles[]
Definition: typeface.cpp:89