Debian Codegraph @

gm/trickycubicstrokes.cpp

raw
1 /*
2 * Copyright 2018 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/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathBuilder.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "src/base/SkRandom.h"
21 #include "src/core/SkGeometry.h"
22
23 static constexpr float kStrokeWidth = 30;
24 static constexpr int kCellSize = 200;
25 static constexpr int kNumCols = 5;
26 static constexpr int kNumRows = 5;
27 static constexpr int kTestWidth = kNumCols * kCellSize;
28 static constexpr int kTestHeight = kNumRows * kCellSize;
29
30 enum class CellFillMode {
31 kStretch,
32 kCenter
33 };
34
35 struct TrickyCubic {
36 SkPoint fPoints[4];
37 int fNumPts;
38 CellFillMode fFillMode;
39 float fScale = 1;
40 };
41
42 // This is a compilation of cubics that have given strokers grief. Feel free to add more.
43 static const TrickyCubic kTrickyCubics[] = {
44 {{{122, 737}, {348, 553}, {403, 761}, {400, 760}}, 4, CellFillMode::kStretch},
45 {{{244, 520}, {244, 518}, {1141, 634}, {394, 688}}, 4, CellFillMode::kStretch},
46 {{{550, 194}, {138, 130}, {1035, 246}, {288, 300}}, 4, CellFillMode::kStretch},
47 {{{226, 733}, {556, 779}, {-43, 471}, {348, 683}}, 4, CellFillMode::kStretch},
48 {{{268, 204}, {492, 304}, {352, 23}, {433, 412}}, 4, CellFillMode::kStretch},
49 {{{172, 480}, {396, 580}, {256, 299}, {338, 677}}, 4, CellFillMode::kStretch},
50 {{{731, 340}, {318, 252}, {1026, -64}, {367, 265}}, 4, CellFillMode::kStretch},
51 {{{475, 708}, {62, 620}, {770, 304}, {220, 659}}, 4, CellFillMode::kStretch},
52 {{{0, 0}, {128, 128}, {128, 0}, {0, 128}}, 4, CellFillMode::kCenter}, // Perfect cusp
53 {{{0,.01f}, {128,127.999f}, {128,.01f}, {0,127.99f}}, 4, CellFillMode::kCenter}, // Near-cusp
54 {{{0,-.01f}, {128,128.001f}, {128,-.01f}, {0,128.001f}}, 4, CellFillMode::kCenter}, // Near-cusp
55 {{{0,0}, {0,-10}, {0,-10}, {0,10}}, 4, CellFillMode::kCenter, 1.098283f}, // Flat line with 180
56 {{{10,0}, {0,0}, {20,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with 2 180s
57 {{{39,-39}, {40,-40}, {40,-40}, {0,0}}, 4, CellFillMode::kStretch}, // Flat diagonal with 180
58 {{{39,-39}, {40,-40}, {37,-39}, {0,0}}, 4, CellFillMode::kStretch}, // Near-flat diagonal
59 {{{40, 40}, {0, 0}, {200, 200}, {0, 0}}, 4, CellFillMode::kStretch}, // Diag w/ an internal 180
60 {{{0,0}, {1e-2f,0}, {-1e-2f,0}, {0,0}}, 4, CellFillMode::kCenter}, // Circle
61 {{{400.75f,100.05f}, {400.75f,100.05f}, {100.05f,300.95f}, {100.05f,300.95f}}, 4,
62 CellFillMode::kStretch}, // Flat line with no turns
63 {{{0.5f,0}, {0,0}, {20,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with 2 180s
64 {{{10,0}, {0,0}, {10,0}, {10,0}}, 4, CellFillMode::kStretch}, // Flat line with a 180
65 {{{1,1}, {2,1}, {1,1}, {1, std::numeric_limits<float>::quiet_NaN()}}, 3,
66 CellFillMode::kStretch}, // Flat QUAD with a cusp
67 {{{1,1}, {100,1}, {25,1}, {.3f, std::numeric_limits<float>::quiet_NaN()}}, 3,
68 CellFillMode::kStretch}, // Flat CONIC with a cusp
69 {{{1,1}, {100,1}, {25,1}, {1.5f, std::numeric_limits<float>::quiet_NaN()}}, 3,
70 CellFillMode::kStretch}, // Flat CONIC with a cusp
71 };
72
73 static SkRect calc_tight_cubic_bounds(const SkPoint P[4], int depth=5) {
74 if (0 == depth) {
75 SkRect bounds;
76 bounds.fLeft = std::min(std::min(P[0].x(), P[1].x()), std::min(P[2].x(), P[3].x()));
77 bounds.fTop = std::min(std::min(P[0].y(), P[1].y()), std::min(P[2].y(), P[3].y()));
78 bounds.fRight = std::max(std::max(P[0].x(), P[1].x()), std::max(P[2].x(), P[3].x()));
79 bounds.fBottom = std::max(std::max(P[0].y(), P[1].y()), std::max(P[2].y(), P[3].y()));
80 return bounds;
81 }
82
83 SkPoint chopped[7];
84 SkChopCubicAt(P, chopped, .5f);
85 SkRect bounds = calc_tight_cubic_bounds(chopped, depth - 1);
86 bounds.join(calc_tight_cubic_bounds(chopped+3, depth - 1));
87 return bounds;
88 }
89
90 static SkPoint lerp(const SkPoint& a, const SkPoint& b, float T) {
91 SkASSERT(1 != T); // The below does not guarantee lerp(a, b, 1) === b.
92 return (b - a) * T + a;
93 }
94
95 enum class FillMode {
96 kCenter,
97 kScale
98 };
99
100 static void draw_test(SkCanvas* canvas, SkPaint::Cap cap, SkPaint::Join join) {
101 SkRandom rand;
102
103 canvas->clear(SK_ColorBLACK);
104
105 SkPaint strokePaint;
106 strokePaint.setAntiAlias(true);
107 strokePaint.setStrokeWidth(kStrokeWidth);
108 strokePaint.setStyle(SkPaint::kStroke_Style);
109 strokePaint.setStrokeCap(cap);
110 strokePaint.setStrokeJoin(join);
111
112 for (size_t i = 0; i < std::size(kTrickyCubics); ++i) {
113 auto [originalPts, numPts, fillMode, scale] = kTrickyCubics[i];
114
115 SkASSERT(numPts <= 4);
116 SkPoint p[4];
117 memcpy(p, originalPts, sizeof(SkPoint) * numPts);
118 for (int j = 0; j < numPts; ++j) {
119 p[j] *= scale;
120 }
121 float w = originalPts[3].fX;
122
123 auto cellRect = SkRect::MakeXYWH((i % kNumCols) * kCellSize, (i / kNumCols) * kCellSize,
124 kCellSize, kCellSize);
125
126 SkRect strokeBounds;
127 if (numPts == 4) {
128 strokeBounds = calc_tight_cubic_bounds(p);
129 } else {
130 SkASSERT(numPts == 3);
131 SkPoint asCubic[4] = {p[0], lerp(p[0], p[1], 2/3.f), lerp(p[1], p[2], 1/3.f), p[2]};
132 strokeBounds = calc_tight_cubic_bounds(asCubic);
133 }
134 strokeBounds.outset(kStrokeWidth, kStrokeWidth);
135
136 SkMatrix matrix = (fillMode == CellFillMode::kStretch) ?
137 SkMatrix::RectToRectOrIdentity(strokeBounds, cellRect, SkMatrix::kCenter_ScaleToFit) :
138 SkMatrix::Translate(cellRect.x() + kStrokeWidth +
139 (cellRect.width() - strokeBounds.width()) / 2,
140 cellRect.y() + kStrokeWidth +
141 (cellRect.height() - strokeBounds.height()) / 2);
142
143 SkAutoCanvasRestore acr(canvas, true);
144 canvas->concat(matrix);
145 strokePaint.setStrokeWidth(kStrokeWidth / matrix.getMaxScale());
146 strokePaint.setColor(rand.nextU() | 0xff808080);
147 SkPathBuilder builder;
148 builder.moveTo(p[0]);
149 if (numPts == 4) {
150 builder.cubicTo(p[1], p[2], p[3]);
151 } else if (w == 1) {
152 SkASSERT(numPts == 3);
153 builder.quadTo(p[1], p[2]);
154 } else {
155 SkASSERT(numPts == 3);
156 builder.conicTo(p[1], p[2], w);
157 }
158 canvas->drawPath(builder.detach(), strokePaint);
159 }
160 }
161
162 DEF_SIMPLE_GM(trickycubicstrokes, canvas, kTestWidth, kTestHeight) {
163 draw_test(canvas, SkPaint::kButt_Cap, SkPaint::kMiter_Join);
164 }
165
166 DEF_SIMPLE_GM(trickycubicstrokes_roundcaps, canvas, kTestWidth, kTestHeight) {
167 draw_test(canvas, SkPaint::kRound_Cap, SkPaint::kRound_Join);
168 }
169
170 // See b/433057370
171 DEF_SIMPLE_GM(trickycubicstrokes_largeradius, canvas, 128, 256) {
172 SkPathBuilder b;
173
174 // Starts as a line with a single tangent direction, with increasing curvature
175 for (int y = 0; y < 2; ++y) {
176 float shift = 210.f * y;
177 float dy = 5.f * y;
178 b.moveTo(159.429f, 149.808f + shift)
179 .cubicTo({232.5f, 149.808f + dy + shift},
180 {232.5f, 149.808f + dy + shift},
181 {305.572f, 149.808f + shift});
182 }
183
184 // A large stroke width is required to show the cusp circle artifacts with
185 // the tessellating path renderer
186 SkPaint s;
187 s.setStroke(true);
188 s.setStrokeWidth(200.f);
189 s.setAntiAlias(true);
190 b.setFillType(SkPathFillType::kWinding);
191 canvas->scale(0.5f, 0.5f);
192 canvas->translate(-125.f, 0.f);
193 canvas->drawPath(b.detach(), s);
194 }