Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
inversepaths.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"
15#include "include/core/SkRect.h"
21
22#include <utility>
23
25 return SkPath::Rect(SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w));
26}
27
29 return SkPath::Rect(SkRect::MakeXYWH(cx - l / 2, cy, l, 0));
30}
31
35
37 return SkPath::Line({cx - l / 2, cy}, {cx + l / 2, cy});
38}
39
40namespace {
41struct Style {
43 : fPaintStyle(paintStyle)
44 , fPathEffect(std::move(pe)) {}
45 SkPaint::Style fPaintStyle;
46 sk_sp<SkPathEffect> fPathEffect;
47};
48
50 constexpr SkScalar kIntervals[] = { 4.f, 3.f };
51 return SkDashPathEffect::Make(kIntervals, std::size(kIntervals), 0);
52}
53
54Style styles[] {
59};
60
61SkScalar pathSizes[] = {
62 40,
63 10,
64 0
65};
66SkScalar strokeWidths[] = {
67 10,
68 0
69};
70SkPath (*paths[])(SkScalar, SkScalar, SkScalar) = {
75};
76
77const SkScalar slideWidth = 90, slideHeight = 90;
78const SkScalar slideBoundary = 5;
79
80} // namespace
81
82DEF_SIMPLE_GM(inverse_paths, canvas, 800, 1200) {
83 SkScalar cx = slideWidth / 2 + slideBoundary;
84 SkScalar cy = slideHeight / 2 + slideBoundary;
85 SkScalar dx = slideWidth + 2 * slideBoundary;
86 SkScalar dy = slideHeight + 2 * slideBoundary;
87
88 SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary,
89 slideBoundary + slideWidth,
90 slideBoundary + slideHeight);
91 SkPaint clipPaint;
93 clipPaint.setStrokeWidth(SkIntToScalar(2));
94
95 SkPaint outlinePaint;
96 outlinePaint.setColor(0x40000000);
97 outlinePaint.setStyle(SkPaint::kStroke_Style);
98 outlinePaint.setStrokeWidth(SkIntToScalar(0));
99
100 for (size_t styleIndex = 0; styleIndex < std::size(styles);
101 styleIndex++) {
102 for (size_t sizeIndex = 0; sizeIndex < std::size(pathSizes);
103 sizeIndex++) {
104 SkScalar size = pathSizes[sizeIndex];
105
106 canvas->save();
107
108 for (size_t widthIndex = 0;
109 widthIndex < std::size(strokeWidths);
110 widthIndex++) {
112 paint.setColor(0xff007000);
113 paint.setStrokeWidth(strokeWidths[widthIndex]);
114 paint.setStyle(styles[styleIndex].fPaintStyle);
115 paint.setPathEffect(styles[styleIndex].fPathEffect);
116
117 for (size_t pathIndex = 0;
118 pathIndex < std::size(paths);
119 pathIndex++) {
120 canvas->drawRect(clipRect, clipPaint);
121
122 canvas->save();
123 canvas->clipRect(clipRect);
124
125 SkPath path = paths[pathIndex](cx, cy, size);
126 path.setFillType(SkPathFillType::kInverseWinding);
127 canvas->drawPath(path, paint);
128
129 path.setFillType(SkPathFillType::kWinding);
130 canvas->drawPath(path, outlinePaint);
131
132 canvas->restore();
133 canvas->translate(dx, 0);
134 }
135 }
136 canvas->restore();
137 canvas->translate(0, dy);
138 }
139 }
140}
141
142DEF_SIMPLE_GM(inverse_fill_filters, canvas, 384, 128) {
143 auto draw = [canvas](const SkPaint& paint) {
144 SkPath path = SkPath::Circle(65.f, 65.f, 30.f);
145 path.setFillType(SkPathFillType::kInverseWinding);
146
147 canvas->save();
148 canvas->clipRect({0, 0, 128, 128});
149 canvas->drawPath(path, paint);
150 canvas->restore();
151
152 SkPaint stroke;
154 stroke.setColor(SK_ColorWHITE);
155 canvas->drawRect({0, 0, 128, 128}, stroke);
156 };
157
159 paint.setAntiAlias(true);
160
161 draw(paint);
162
163 canvas->translate(128, 0);
164 paint.setImageFilter(SkImageFilters::Blur(5.f, 5.f, nullptr));
165 draw(paint);
166
167 canvas->translate(128, 0);
168 paint.setImageFilter(nullptr);
170 draw(paint);
171}
172
173DEF_SIMPLE_GM(inverse_windingmode_filters, canvas, 256, 100) {
174 SkPath path;
175 path.addRect({10, 10, 30, 30}, SkPathDirection::kCW);
176 path.addRect({20, 20, 40, 40}, SkPathDirection::kCW);
177 path.addRect({10, 60, 30, 80}, SkPathDirection::kCW);
178 path.addRect({20, 70, 40, 90}, SkPathDirection::kCCW);
179 SkPaint strokePaint;
180 strokePaint.setStyle(SkPaint::kStroke_Style);
181 SkRect clipRect = {0, 0, 51, 99};
182 canvas->drawPath(path, strokePaint);
183 SkPaint fillPaint;
185 for (auto fillType : { SkPathFillType::kWinding,
189 canvas->translate(51, 0);
190 canvas->save();
191 canvas->clipRect(clipRect);
192 path.setFillType(fillType);
193 canvas->drawPath(path, fillPaint);
194 canvas->restore();
195 SkPaint clipPaint;
196 clipPaint.setColor(SK_ColorRED);
198 clipPaint.setStrokeWidth(1.f);
199 canvas->drawRect(clipRect, clipPaint);
200 }
201}
static sk_sp< SkPathEffect > make_dash()
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition SkBlurTypes.h:12
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkIntToScalar(x)
Definition SkScalar.h:57
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
@ 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
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
static SkPath Rect(const SkRect &, SkPathDirection=SkPathDirection::kCW, unsigned startIndex=0)
Definition SkPath.cpp:3518
static SkPath Circle(SkScalar center_x, SkScalar center_y, SkScalar radius, SkPathDirection dir=SkPathDirection::kCW)
Definition SkPath.cpp:3530
static SkPath Line(const SkPoint a, const SkPoint b)
Definition SkPath.h:106
const Paint & paint
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d)
static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l)
static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w)
static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l)
Definition ref_ptr.h:256
SkScalar w
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646