Flutter Engine
The Flutter Engine
cubicpaths.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"
14#include "include/core/SkPath.h"
16#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
23#include "src/base/SkRandom.h"
24#include "tools/ToolUtils.h"
26
27// https://bug.skia.org/1316 shows that this cubic, when slightly clipped, creates big
28// (incorrect) changes to its control points.
29class ClippedCubicGM : public skiagm::GM {
30 SkString getName() const override { return SkString("clippedcubic"); }
31
32 SkISize getISize() override { return {1240, 390}; }
33
34 void onDraw(SkCanvas* canvas) override {
36 path.moveTo(0, 0);
37 path.cubicTo(140, 150, 40, 10, 170, 150);
38
40 SkRect bounds = path.getBounds();
41
42 for (SkScalar dy = -1; dy <= 1; dy += 1) {
43 canvas->save();
44 for (SkScalar dx = -1; dx <= 1; dx += 1) {
45 canvas->save();
46 canvas->clipRect(bounds);
47 canvas->translate(dx, dy);
48 canvas->drawPath(path, paint);
49 canvas->restore();
50
51 canvas->translate(bounds.width(), 0);
52 }
53 canvas->restore();
54 canvas->translate(0, bounds.height());
55 }
56 }
57};
58
59
61 SkString getName() const override { return SkString("clippedcubic2"); }
62
63 SkISize getISize() override { return {1240, 390}; }
64
65 void onDraw(SkCanvas* canvas) override {
66 canvas->save();
67 canvas->translate(-2, 120);
68 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 80, 150));
69 canvas->translate(0, 170);
70 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 80, 100));
71 canvas->translate(0, 170);
72 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 30, 150));
73 canvas->translate(0, 170);
74 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 10, 150));
75 canvas->restore();
76 canvas->save();
77 canvas->translate(20, -2);
78 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 150, 80));
79 canvas->translate(170, 0);
80 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 100, 80));
81 canvas->translate(170, 0);
82 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 150, 30));
83 canvas->translate(170, 0);
84 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 150, 10));
85 canvas->restore();
86 }
87
88 void drawOne(SkCanvas* canvas, const SkPath& path, const SkRect& clip) {
89 SkPaint framePaint, fillPaint;
91 canvas->drawRect(clip, framePaint);
92 canvas->drawPath(path, framePaint);
93 canvas->save();
94 canvas->clipRect(clip);
95 canvas->drawPath(path, fillPaint);
96 canvas->restore();
97 }
98
99 void onOnceBeforeDraw() override {
100 fPath.moveTo(69.7030518991886f, 0);
101 fPath.cubicTo( 69.7030518991886f, 21.831149999999997f,
102 58.08369508178456f, 43.66448333333333f, 34.8449814469765f, 65.5f);
103 fPath.cubicTo( 11.608591683531916f, 87.33115f, -0.010765133872116195f, 109.16448333333332f,
104 -0.013089005235602302f, 131);
105 fPath.close();
106 fFlipped = fPath;
108 matrix.reset();
109 matrix.setScaleX(0);
110 matrix.setScaleY(0);
111 matrix.setSkewX(1);
112 matrix.setSkewY(1);
113 fFlipped.transform(matrix);
114 }
115
116 SkPath fPath;
117 SkPath fFlipped;
118private:
119 using INHERITED = skiagm::GM;
120};
121
122class CubicPathGM : public skiagm::GM {
123 SkString getName() const override { return SkString("cubicpath"); }
124
125 SkISize getISize() override { return {1240, 390}; }
126
127 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
129 SkPaint::Style style, SkPathFillType fill,
131 path.setFillType(fill);
133 paint.setStrokeCap(cap);
134 paint.setStrokeWidth(strokeWidth);
135 paint.setStrokeJoin(join);
136 paint.setColor(color);
137 paint.setStyle(style);
138 canvas->save();
139 canvas->clipRect(clip);
140 canvas->drawPath(path, paint);
141 canvas->restore();
142 }
143
144 void onDraw(SkCanvas* canvas) override {
145 struct FillAndName {
146 SkPathFillType fFill;
147 const char* fName;
148 };
149 constexpr FillAndName gFills[] = {
150 {SkPathFillType::kWinding, "Winding"},
151 {SkPathFillType::kEvenOdd, "Even / Odd"},
152 {SkPathFillType::kInverseWinding, "Inverse Winding"},
153 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
154 };
155 struct StyleAndName {
157 const char* fName;
158 };
159 constexpr StyleAndName gStyles[] = {
160 {SkPaint::kFill_Style, "Fill"},
161 {SkPaint::kStroke_Style, "Stroke"},
162 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
163 };
164 struct CapAndName {
165 SkPaint::Cap fCap;
167 const char* fName;
168 };
169 constexpr CapAndName gCaps[] = {
173 };
174 struct PathAndName {
176 const char* fName;
177 };
178 PathAndName path;
179 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
180 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
181 60*SK_Scalar1, 20*SK_Scalar1,
182 75*SK_Scalar1, 10*SK_Scalar1);
183 path.fName = "moveTo-cubic";
184
185 SkPaint titlePaint;
186 titlePaint.setColor(SK_ColorBLACK);
187 titlePaint.setAntiAlias(true);
189 const char title[] = "Cubic Drawn Into Rectangle Clips With "
190 "Indicated Style, Fill and Linecaps, with stroke width 10";
191 canvas->drawString(title, 20, 20, font, titlePaint);
192
193 SkRandom rand;
195 canvas->save();
196 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
197 canvas->save();
198 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
199 if (0 < cap) {
200 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
201 }
202 canvas->save();
203 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
204 if (0 < fill) {
205 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
206 }
207 canvas->save();
208 for (size_t style = 0; style < std::size(gStyles); ++style) {
209 if (0 < style) {
210 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
211 }
212
213 SkColor color = 0xff007000;
214 this->drawPath(path.fPath, canvas, color, rect,
215 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
216 gFills[fill].fFill, SK_Scalar1*10);
217
218 SkPaint rectPaint;
219 rectPaint.setColor(SK_ColorBLACK);
221 rectPaint.setStrokeWidth(-1);
222 rectPaint.setAntiAlias(true);
223 canvas->drawRect(rect, rectPaint);
224
225 SkPaint labelPaint;
226 labelPaint.setColor(color);
227 font.setSize(10);
228 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12, font, labelPaint);
229 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24, font, labelPaint);
230 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36, font, labelPaint);
231 }
232 canvas->restore();
233 }
234 canvas->restore();
235 }
236 canvas->restore();
237 canvas->restore();
238 }
239};
240
242 SkString getName() const override { return SkString("cubicclosepath"); }
243
244 SkISize getISize() override { return {1240, 390}; }
245
246 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
248 SkPaint::Style style, SkPathFillType fill,
250 path.setFillType(fill);
252 paint.setStrokeCap(cap);
253 paint.setStrokeWidth(strokeWidth);
254 paint.setStrokeJoin(join);
255 paint.setColor(color);
256 paint.setStyle(style);
257 canvas->save();
258 canvas->clipRect(clip);
259 canvas->drawPath(path, paint);
260 canvas->restore();
261 }
262
263 void onDraw(SkCanvas* canvas) override {
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"},
281 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
282 };
283 struct CapAndName {
284 SkPaint::Cap fCap;
286 const char* fName;
287 };
288 constexpr CapAndName gCaps[] = {
292 };
293 struct PathAndName {
295 const char* fName;
296 };
297 PathAndName path;
298 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
299 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
300 60*SK_Scalar1, 20*SK_Scalar1,
301 75*SK_Scalar1, 10*SK_Scalar1);
302 path.fPath.close();
303 path.fName = "moveTo-cubic-close";
304
305 SkPaint titlePaint;
306 titlePaint.setColor(SK_ColorBLACK);
307 titlePaint.setAntiAlias(true);
309 const char title[] = "Cubic Closed Drawn Into Rectangle Clips With "
310 "Indicated Style, Fill and Linecaps, with stroke width 10";
311 canvas->drawString(title, 20, 20, font, titlePaint);
312
313 SkRandom rand;
315 canvas->save();
316 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
317 canvas->save();
318 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
319 if (0 < cap) {
320 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
321 }
322 canvas->save();
323 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
324 if (0 < fill) {
325 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
326 }
327 canvas->save();
328 for (size_t style = 0; style < std::size(gStyles); ++style) {
329 if (0 < style) {
330 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
331 }
332
333 SkColor color = 0xff007000;
334 this->drawPath(path.fPath, canvas, color, rect,
335 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
336 gFills[fill].fFill, SK_Scalar1*10);
337
338 SkPaint rectPaint;
339 rectPaint.setColor(SK_ColorBLACK);
341 rectPaint.setStrokeWidth(-1);
342 rectPaint.setAntiAlias(true);
343 canvas->drawRect(rect, rectPaint);
344
345 SkPaint labelPaint;
346 labelPaint.setColor(color);
347 labelPaint.setAntiAlias(true);
348 font.setSize(10);
349 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12, font, labelPaint);
350 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24, font, labelPaint);
351 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36, font, labelPaint);
352 }
353 canvas->restore();
354 }
355 canvas->restore();
356 }
357 canvas->restore();
358 canvas->restore();
359 }
360};
361
363 SkString getName() const override { return SkString("cubicpath_shader"); }
364
365 SkISize getISize() override { return {1240, 390}; }
366
367 void drawPath(SkPath& path,SkCanvas* canvas,
369 SkPaint::Style style, SkPathFillType fill,
371 const SkScalar s = 50.f;
372 const SkPoint kPts[] = { { 0, 0 }, { s, s } };
373 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
374 const SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
375
376 path.setFillType(fill);
377
379 paint.setStrokeCap(cap);
380 paint.setStrokeWidth(strokeWidth);
381 paint.setStrokeJoin(join);
384 paint.setStyle(style);
385 canvas->save();
386 canvas->clipRect(clip);
387 canvas->drawPath(path, paint);
388 canvas->restore();
389 }
390
391 void onDraw(SkCanvas* canvas) override {
392 struct FillAndName {
393 SkPathFillType fFill;
394 const char* fName;
395 };
396 constexpr FillAndName gFills[] = {
397 {SkPathFillType::kWinding, "Winding"},
398 {SkPathFillType::kEvenOdd, "Even / Odd"},
399 {SkPathFillType::kInverseWinding, "Inverse Winding"},
400 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
401 };
402 struct StyleAndName {
404 const char* fName;
405 };
406 constexpr StyleAndName gStyles[] = {
407 {SkPaint::kFill_Style, "Fill"},
408 {SkPaint::kStroke_Style, "Stroke"},
409 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
410 };
411 struct CapAndName {
412 SkPaint::Cap fCap;
414 const char* fName;
415 };
416 constexpr CapAndName gCaps[] = {
420 };
421 struct PathAndName {
423 const char* fName;
424 };
425 PathAndName path;
426 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
427 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
428 60*SK_Scalar1, 20*SK_Scalar1,
429 75*SK_Scalar1, 10*SK_Scalar1);
430 path.fName = "moveTo-cubic";
431
432 SkPaint titlePaint;
433 titlePaint.setColor(SK_ColorBLACK);
434 titlePaint.setAntiAlias(true);
436 const char title[] = "Cubic Drawn Into Rectangle Clips With "
437 "Indicated Style, Fill and Linecaps, with stroke width 10";
438 canvas->drawString(title, 20, 20, font, titlePaint);
439
440 SkRandom rand;
442 canvas->save();
443 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
444 canvas->save();
445 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
446 if (0 < cap) {
447 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
448 }
449 canvas->save();
450 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
451 if (0 < fill) {
452 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
453 }
454 canvas->save();
455 for (size_t style = 0; style < std::size(gStyles); ++style) {
456 if (0 < style) {
457 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
458 }
459
460 SkColor color = 0xff007000;
461 this->drawPath(path.fPath, canvas, rect,
462 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
463 gFills[fill].fFill, SK_Scalar1*10);
464
465 SkPaint rectPaint;
466 rectPaint.setColor(SK_ColorBLACK);
468 rectPaint.setStrokeWidth(-1);
469 rectPaint.setAntiAlias(true);
470 canvas->drawRect(rect, rectPaint);
471
472 SkPaint labelPaint;
473 labelPaint.setColor(color);
474 font.setSize(10);
475 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12, font, labelPaint);
476 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24, font, labelPaint);
477 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36, font, labelPaint);
478 }
479 canvas->restore();
480 }
481 canvas->restore();
482 }
483 canvas->restore();
484 canvas->restore();
485 }
486};
487
488DEF_SIMPLE_GM(bug5099, canvas, 50, 50) {
489 SkPaint p;
490 p.setColor(SK_ColorRED);
491 p.setAntiAlias(true);
492 p.setStyle(SkPaint::kStroke_Style);
493 p.setStrokeWidth(10);
494
495 SkPath path;
496 path.moveTo(6, 27);
497 path.cubicTo(31.5f, 1.5f, 3.5f, 4.5f, 29, 29);
498 canvas->drawPath(path, p);
499}
500
501DEF_SIMPLE_GM(bug6083, canvas, 100, 50) {
502 SkPaint p;
503 p.setColor(SK_ColorRED);
504 p.setAntiAlias(true);
505 p.setStyle(SkPaint::kStroke_Style);
506 p.setStrokeWidth(15);
507 canvas->translate(-500, -130);
508 SkPath path;
509 path.moveTo(500.988f, 155.200f);
510 path.lineTo(526.109f, 155.200f);
511 SkPoint p1 = { 526.109f, 155.200f };
512 SkPoint p2 = { 525.968f, 212.968f };
513 SkPoint p3 = { 526.109f, 241.840f };
514 path.cubicTo(p1, p2, p3);
515 canvas->drawPath(path, p);
516 canvas->translate(50, 0);
517 path.reset();
518 p2.set(525.968f, 213.172f);
519 path.moveTo(500.988f, 155.200f);
520 path.lineTo(526.109f, 155.200f);
521 path.cubicTo(p1, p2, p3);
522 canvas->drawPath(path, p);
523}
524
525//////////////////////////////////////////////////////////////////////////////
526
527DEF_GM( return new CubicPathGM; )
528DEF_GM( return new CubicPathShaderGM; )
529DEF_GM( return new CubicClosePathGM; )
530DEF_GM( return new ClippedCubicGM; )
531DEF_GM( return new ClippedCubic2GM; )
SkPath fPath
SkPaint::Join fJoin
SkStrokeRec::Style fStyle
static const int strokeWidth
Definition: BlurTest.cpp:60
const char * fName
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
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
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
@ 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
SkPath & moveTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:688
SkPath & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPath.cpp:799
SkPath & close()
Definition: SkPath.cpp:823
void transform(const SkMatrix &matrix, SkPath *dst, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkPath.cpp:1711
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
DEF_SIMPLE_GM(bug5099, canvas, 50, 50)
Definition: cubicpaths.cpp:488
DlColor color
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
#define DEF_GM(CODE)
Definition: gm.h:40
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
sk_sp< SkTypeface > DefaultPortableTypeface()
const DlColor kColors[]
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.
static constexpr SkPoint kPts[kPtsCount]
Definition: emptypath.cpp:123
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741
Definition: SkSize.h:16
void set(float x, float y)
Definition: SkPoint_impl.h:200
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
static constexpr SkFontStyle gStyles[]
Definition: typeface.cpp:89