Debian Codegraph @

gm/strokedlines.cpp

raw
1 /*
2 * Copyright 2016 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"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlurTypes.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkMaskFilter.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPath.h"
17 #include "include/core/SkPathEffect.h"
18 #include "include/core/SkPathUtils.h"
19 #include "include/core/SkPoint.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkDashPathEffect.h"
28 #include "include/effects/SkGradient.h"
29 #include "include/private/base/SkTArray.h"
30 #include "include/private/base/SkTemplates.h"
31 #include "tools/ToolUtils.h"
32
33 #include <initializer_list>
34
35 using namespace skia_private;
36
37 constexpr int kNumColumns = 6;
38 constexpr int kNumRows = 8;
39 constexpr int kRadius = 40; // radius of the snowflake
40 constexpr int kPad = 5; // padding on both sides of the snowflake
41 constexpr int kNumSpokes = 6;
42 constexpr SkScalar kStrokeWidth = 5.0f;
43
44 static void draw_line(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1,
45 const SkPaint& paint, bool useDrawPath) {
46 if (useDrawPath) {
47 canvas->drawPath(SkPath::Line(p0, p1), paint);
48 } else {
49 canvas->drawLine(p0, p1, paint);
50 }
51 }
52
53 static void draw_fins(SkCanvas* canvas, const SkPoint& offset, float angle, const SkPaint& paint,
54 bool useDrawPath) {
55 SkScalar cos, sin;
56
57 // first fin
58 sin = SkScalarSin(angle + (SK_ScalarPI/4));
59 cos = SkScalarCos(angle + (SK_ScalarPI/4));
60 sin *= kRadius / 2.0f;
61 cos *= kRadius / 2.0f;
62
63 draw_line(canvas, offset, offset + SkPoint{cos, sin}, paint, useDrawPath);
64
65 // second fin
66 sin = SkScalarSin(angle - (SK_ScalarPI/4));
67 cos = SkScalarCos(angle - (SK_ScalarPI/4));
68 sin *= kRadius / 2.0f;
69 cos *= kRadius / 2.0f;
70
71 draw_line(canvas, offset, offset + SkPoint{cos, sin}, paint, useDrawPath);
72 }
73
74 // draw a snowflake centered at the origin
75 static void draw_snowflake(SkCanvas* canvas, const SkPaint& paint, bool useDrawPath) {
76
77 canvas->clipRect(SkRect::MakeLTRB(-kRadius-kPad, -kRadius-kPad, kRadius+kPad, kRadius+kPad));
78
79 SkScalar sin, cos, angle = 0.0f;
80 for (int i = 0; i < kNumSpokes/2; ++i, angle += SK_ScalarPI/(kNumSpokes/2)) {
81 sin = SkScalarSin(angle);
82 cos = SkScalarCos(angle);
83 sin *= kRadius;
84 cos *= kRadius;
85
86 // main spoke
87 draw_line(canvas, {-cos, -sin}, {cos, sin}, paint, useDrawPath);
88
89 // fins on positive side
90 const SkPoint posOffset = SkPoint::Make(0.5f * cos, 0.5f * sin);
91 draw_fins(canvas, posOffset, angle, paint, useDrawPath);
92
93 // fins on negative side
94 const SkPoint negOffset = SkPoint::Make(-0.5f * cos, -0.5f * sin);
95 draw_fins(canvas, negOffset, angle+SK_ScalarPI, paint, useDrawPath);
96 }
97 }
98
99 static void draw_row(SkCanvas* canvas, const SkPaint& paint, const SkMatrix& localMatrix,
100 bool useDrawPath) {
101 canvas->translate(kRadius+kPad, 0.0f);
102
103 for (auto cap : { SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap }) {
104 for (auto isAA : { true, false }) {
105 SkPaint tmp(paint);
106 tmp.setStrokeWidth(kStrokeWidth);
107 tmp.setStyle(SkPaint::kStroke_Style);
108 tmp.setStrokeCap(cap);
109 tmp.setAntiAlias(isAA);
110
111 int saveCount = canvas->save();
112 canvas->concat(localMatrix);
113 draw_snowflake(canvas, tmp, useDrawPath);
114 canvas->restoreToCount(saveCount);
115
116 canvas->translate(2*(kRadius+kPad), 0.0f);
117 }
118 }
119 }
120
121 namespace skiagm {
122
123 // This GM exercises the special case of a stroked lines.
124 // Various shaders are applied to ensure the coordinate spaces work out right.
125 class StrokedLinesGM : public GM {
126 public:
127 StrokedLinesGM(bool useDrawPath) : fUseDrawPath(useDrawPath) {
128 this->setBGColor(ToolUtils::color_to_565(0xFF1A65D7));
129 }
130
131 protected:
132 SkString getName() const override {
133 // To preserve history, useDrawPath==true has no suffix.
134 SkString name{"strokedlines"};
135 if (!fUseDrawPath) {
136 name.append("_drawPoints");
137 }
138 return name;
139 }
140
141 SkISize getISize() override {
142 return SkISize::Make(kNumColumns * (2*kRadius+2*kPad), kNumRows * (2*kRadius+2*kPad));
143 }
144
145 void onOnceBeforeDraw() override {
146 // paints
147 {
148 // basic white
149 SkPaint p;
150 p.setColor(SK_ColorWHITE);
151 fPaints.push_back(p);
152 }
153 {
154 // gradient
155 SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen };
156 SkPoint pts[] = { {-kRadius-kPad, -kRadius-kPad }, { kRadius+kPad, kRadius+kPad } };
157
158 SkPaint p;
159 p.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}}));
160
161 fPaints.push_back(p);
162 }
163 {
164 // dashing
165 SkScalar intervals[] = { kStrokeWidth, kStrokeWidth };
166 SkPaint p;
167 p.setColor(SK_ColorWHITE);
168 p.setPathEffect(SkDashPathEffect::Make(intervals, kStrokeWidth));
169
170 fPaints.push_back(p);
171 }
172 {
173 // Bitmap shader
174 SkBitmap bm;
175 bm.allocN32Pixels(2, 2);
176 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
177 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = 0x0;
178
179 SkMatrix m;
180 m.setRotate(12.0f);
181 m.preScale(3.0f, 3.0f);
182
183 SkPaint p;
184 p.setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
185 SkSamplingOptions(), m));
186 fPaints.push_back(p);
187 }
188 {
189 // blur
190 SkPaint p;
191 p.setColor(SK_ColorWHITE);
192 p.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3.0f));
193 fPaints.push_back(p);
194 }
195
196 // matrices
197 {
198 // rotation
199 SkMatrix m;
200 m.setRotate(12.0f);
201
202 fMatrices.push_back(m);
203 }
204 {
205 // skew
206 SkMatrix m;
207 m.setSkew(0.3f, 0.5f);
208
209 fMatrices.push_back(m);
210 }
211 {
212 // perspective
213 SkMatrix m;
214 m.reset();
215 m.setPerspX(-SK_Scalar1 / 300);
216 m.setPerspY(SK_Scalar1 / 300);
217
218 fMatrices.push_back(m);
219 }
220
221 SkASSERT(kNumRows == fPaints.size() + fMatrices.size());
222 }
223
224 void onDraw(SkCanvas* canvas) override {
225 canvas->translate(0, kRadius+kPad);
226
227 for (int i = 0; i < fPaints.size(); ++i) {
228 int saveCount = canvas->save();
229 draw_row(canvas, fPaints[i], SkMatrix::I(), fUseDrawPath);
230 canvas->restoreToCount(saveCount);
231
232 canvas->translate(0, 2*(kRadius+kPad));
233 }
234
235 for (int i = 0; i < fMatrices.size(); ++i) {
236 int saveCount = canvas->save();
237 draw_row(canvas, fPaints[0], fMatrices[i], fUseDrawPath);
238 canvas->restoreToCount(saveCount);
239
240 canvas->translate(0, 2*(kRadius+kPad));
241 }
242 }
243
244 private:
245 TArray<SkPaint> fPaints;
246 TArray<SkMatrix> fMatrices;
247
248 const bool fUseDrawPath;
249
250 using INHERITED = GM;
251 };
252
253 //////////////////////////////////////////////////////////////////////////////
254
255 DEF_GM(return new StrokedLinesGM(true);)
256 DEF_GM(return new StrokedLinesGM(false);)
257
258 //////////////////////////////////////////////////////////////////////////////
259
260 static constexpr float kStrokeWidth = 20.f;
261
262 static void draw_path(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1, SkPaint::Cap cap) {
263 // Add a gradient *not* aligned with the line's points to show local coords are tracked properly
264 constexpr SkRect kRect {-kStrokeWidth, -kStrokeWidth, 2*kStrokeWidth, 4*kStrokeWidth};
265 constexpr SkPoint kPts[] {{kRect.fLeft, kRect.fTop}, {kRect.fRight, kRect.fBottom}};
266 constexpr SkColor4f kColors[] {SkColors::kRed, SkColors::kGreen, SkColors::kBlue};
267 constexpr SkScalar kStops[] {0.f, 0.75f, 1.f};
268 sk_sp<SkShader> shader = SkShaders::LinearGradient(kPts,
269 {{kColors, kStops, SkTileMode::kClamp}, {}});
270
271 SkPaint paint;
272 paint.setAntiAlias(true);
273 paint.setStyle(SkPaint::kStroke_Style);
274
275 paint.setShader(std::move(shader));
276 paint.setStrokeWidth(kStrokeWidth);
277 paint.setStrokeCap(cap);
278 canvas->drawLine(p0, p1, paint);
279
280 // Show outline and control points
281 SkPath fillPath = skpathutils::FillPathWithPaint(SkPath::Line(p0, p1), paint);
282
283 paint.setStyle(SkPaint::kStroke_Style);
284 paint.setStrokeWidth(0);
285 paint.setShader(nullptr);
286 paint.setColor(SK_ColorRED);
287 canvas->drawPath(fillPath, paint);
288
289 paint.setStrokeWidth(3);
290 paint.setStrokeCap(SkPaint::kSquare_Cap);
291 canvas->drawPoints(SkCanvas::kPoints_PointMode, fillPath.points(), paint);
292 }
293
294 DEF_SIMPLE_GM(strokedline_caps, canvas, 1400, 740) {
295 canvas->translate(kStrokeWidth*3/2, kStrokeWidth*3/2);
296
297 constexpr SkPaint::Cap kCaps[] = {
298 SkPaint::kSquare_Cap, SkPaint::kButt_Cap, SkPaint::kRound_Cap
299 };
300
301 constexpr float kLengths[] = {
302 4*kStrokeWidth, kStrokeWidth, kStrokeWidth/2, kStrokeWidth/4
303 };
304
305 for (size_t i = 0; i < std::size(kCaps); ++i) {
306 SkAutoCanvasRestore acr(canvas, true);
307
308 auto drawLine = [&](float x0, float y0, float x1, float y1) {
309 draw_path(canvas, {x0, y0}, {x1, y1}, kCaps[i]);
310 canvas->translate(std::max(x0, x1) + 2 * kStrokeWidth, 0);
311 };
312
313 for (size_t j = 0; j < std::size(kLengths); ++j) {
314 float l = kLengths[j];
315
316 drawLine(0.f, 0.f, l, l);
317 drawLine(l, l, 0.f, 0.f);
318 drawLine(l/2, 0, l/2, l);
319 drawLine(0, l/2, l, l/2);
320 }
321
322 drawLine(kStrokeWidth/2, kStrokeWidth/2, kStrokeWidth/2, kStrokeWidth/2);
323
324 acr.restore();
325 canvas->translate(0, kLengths[0] + 2 * kStrokeWidth);
326 }
327 }
328
329 } // namespace skiagm