blob: aba1ef27ce4e09fa75aeb0ba013e998b4b2da8f7 [file] [log] [blame]
robertphillips72729352015-04-28 07:42:04 -07001/*
2 * Copyright 2015 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.h"
reed026beb52015-06-10 14:23:15 -07009#include "SkPathPriv.h"
robertphillips72729352015-04-28 07:42:04 -070010
11static void create_ngon(int n, SkPoint* pts, SkScalar width, SkScalar height) {
12 float angleStep = 360.0f / n, angle = 0.0f, sin, cos;
13 if ((n % 2) == 1) {
14 angle = angleStep/2.0f;
15 }
16
17 for (int i = 0; i < n; ++i) {
18 sin = SkScalarSinCos(SkDegreesToRadians(angle), &cos);
19 pts[i].fX = -sin * width;
20 pts[i].fY = cos * height;
21 angle += angleStep;
22 }
23}
24
25namespace skiagm {
26
27// This GM is intended to exercise Ganesh's handling of convex line-only
28// paths
29class ConvexLineOnlyPathsGM : public GM {
30public:
robertphillipsad234462016-08-26 05:30:19 -070031 ConvexLineOnlyPathsGM(bool doStrokeAndFill) : fDoStrokeAndFill(doStrokeAndFill) {
robertphillips72729352015-04-28 07:42:04 -070032 this->setBGColor(0xFFFFFFFF);
33 }
34
35protected:
robertphillipsad234462016-08-26 05:30:19 -070036 SkString onShortName() override {
37 if (fDoStrokeAndFill) {
38 return SkString("convex-lineonly-paths-stroke-and-fill");
39 }
40 return SkString("convex-lineonly-paths");
41 }
robertphillips72729352015-04-28 07:42:04 -070042 SkISize onISize() override { return SkISize::Make(kGMWidth, kGMHeight); }
43 bool runAsBench() const override { return true; }
44
45 static SkPath GetPath(int index, int offset, SkPath::Direction dir) {
46 // narrow rect
47 const SkPoint gPoints0[] = {
48 { -1.5f, -50.0f },
49 { 1.5f, -50.0f },
50 { 1.5f, 50.0f },
51 { -1.5f, 50.0f }
52 };
53 // narrow rect on an angle
54 const SkPoint gPoints1[] = {
55 { -50.0f, -49.0f },
56 { -49.0f, -50.0f },
57 { 50.0f, 49.0f },
58 { 49.0f, 50.0f }
59 };
60 // trap - narrow on top - wide on bottom
61 const SkPoint gPoints2[] = {
62 { -10.0f, -50.0f },
63 { 10.0f, -50.0f },
64 { 50.0f, 50.0f },
65 { -50.0f, 50.0f }
66 };
67 // wide skewed rect
68 const SkPoint gPoints3[] = {
69 { -50.0f, -50.0f },
70 { 0.0f, -50.0f },
71 { 50.0f, 50.0f },
72 { 0.0f, 50.0f }
73 };
74 // thin rect with colinear-ish lines
75 const SkPoint gPoints4[] = {
76 { -6.0f, -50.0f },
77 { 4.0f, -50.0f },
78 { 5.0f, -25.0f },
79 { 6.0f, 0.0f },
80 { 5.0f, 25.0f },
81 { 4.0f, 50.0f },
82 { -4.0f, 50.0f }
83 };
84 // degenerate
85 const SkPoint gPoints5[] = {
86 { -0.025f, -0.025f },
87 { 0.025f, -0.025f },
88 { 0.025f, 0.025f },
89 { -0.025f, 0.025f }
90 };
91 // Triangle in which the first point should fuse with last
92 const SkPoint gPoints6[] = {
93 { -20.0f, -13.0f },
94 { -20.0f, -13.05f },
95 { 20.0f, -13.0f },
96 { 20.0f, 27.0f }
97 };
98 // thin rect with colinear lines
99 const SkPoint gPoints7[] = {
100 { -10.0f, -50.0f },
101 { 10.0f, -50.0f },
102 { 10.0f, -25.0f },
103 { 10.0f, 0.0f },
104 { 10.0f, 25.0f },
105 { 10.0f, 50.0f },
106 { -10.0f, 50.0f }
107 };
108 // capped teardrop
109 const SkPoint gPoints8[] = {
110 { 50.00f, 50.00f },
111 { 0.00f, 50.00f },
112 { -15.45f, 47.55f },
113 { -29.39f, 40.45f },
114 { -40.45f, 29.39f },
115 { -47.55f, 15.45f },
116 { -50.00f, 0.00f },
117 { -47.55f, -15.45f },
118 { -40.45f, -29.39f },
119 { -29.39f, -40.45f },
120 { -15.45f, -47.55f },
121 { 0.00f, -50.00f },
122 { 50.00f, -50.00f }
123 };
124 // teardrop
125 const SkPoint gPoints9[] = {
126 { 4.39f, 40.45f },
127 { -9.55f, 47.55f },
128 { -25.00f, 50.00f },
129 { -40.45f, 47.55f },
130 { -54.39f, 40.45f },
131 { -65.45f, 29.39f },
132 { -72.55f, 15.45f },
133 { -75.00f, 0.00f },
134 { -72.55f, -15.45f },
135 { -65.45f, -29.39f },
136 { -54.39f, -40.45f },
137 { -40.45f, -47.55f },
138 { -25.0f, -50.0f },
139 { -9.55f, -47.55f },
140 { 4.39f, -40.45f },
141 { 75.00f, 0.00f }
142 };
143 // clipped triangle
144 const SkPoint gPoints10[] = {
145 { -10.0f, -50.0f },
146 { 10.0f, -50.0f },
147 { 50.0f, 31.0f },
148 { 40.0f, 50.0f },
149 { -40.0f, 50.0f },
150 { -50.0f, 31.0f },
151 };
152
153 const SkPoint* gPoints[] = {
154 gPoints0, gPoints1, gPoints2, gPoints3, gPoints4, gPoints5, gPoints6,
155 gPoints7, gPoints8, gPoints9, gPoints10,
156 };
157
158 const size_t gSizes[] = {
159 SK_ARRAY_COUNT(gPoints0),
160 SK_ARRAY_COUNT(gPoints1),
161 SK_ARRAY_COUNT(gPoints2),
162 SK_ARRAY_COUNT(gPoints3),
163 SK_ARRAY_COUNT(gPoints4),
164 SK_ARRAY_COUNT(gPoints5),
165 SK_ARRAY_COUNT(gPoints6),
166 SK_ARRAY_COUNT(gPoints7),
167 SK_ARRAY_COUNT(gPoints8),
168 SK_ARRAY_COUNT(gPoints9),
169 SK_ARRAY_COUNT(gPoints10),
170 };
bungeman99fe8222015-08-20 07:57:51 -0700171 static_assert(SK_ARRAY_COUNT(gSizes) == SK_ARRAY_COUNT(gPoints), "array_mismatch");
robertphillips72729352015-04-28 07:42:04 -0700172
halcanary96fcdcc2015-08-27 07:41:13 -0700173 SkAutoTDeleteArray<SkPoint> data(nullptr);
robertphillips72729352015-04-28 07:42:04 -0700174 const SkPoint* points;
175 int numPts;
176 if (index < (int) SK_ARRAY_COUNT(gPoints)) {
177 // manually specified
178 points = gPoints[index];
179 numPts = (int) gSizes[index];
180 } else {
181 // procedurally generated
182 SkScalar width = kMaxPathHeight/2;
183 SkScalar height = kMaxPathHeight/2;
184 switch (index-SK_ARRAY_COUNT(gPoints)) {
185 case 0:
186 numPts = 3;
187 break;
188 case 1:
189 numPts = 4;
190 break;
191 case 2:
192 numPts = 5;
193 break;
194 case 3: // squashed pentagon
195 numPts = 5;
196 width = kMaxPathHeight/5;
197 break;
198 case 4:
199 numPts = 6;
200 break;
201 case 5:
202 numPts = 8;
203 break;
204 case 6: // squashed octogon
205 numPts = 8;
206 width = kMaxPathHeight/5;
207 break;
208 case 7:
209 numPts = 20;
210 break;
211 case 8:
212 numPts = 100;
213 break;
214 default:
215 numPts = 3;
216 break;
217 }
218
halcanary385fe4d2015-08-26 13:07:48 -0700219 data.reset(new SkPoint[numPts]);
robertphillips72729352015-04-28 07:42:04 -0700220
221 create_ngon(numPts, data.get(), width, height);
222 points = data.get();
223 }
224
225 SkPath path;
226
227 if (SkPath::kCW_Direction == dir) {
228 path.moveTo(points[0]);
229 for (int i = 1; i < numPts; ++i) {
230 path.lineTo(points[i]);
231 }
232 } else {
233 path.moveTo(points[numPts-1]);
234 for (int i = numPts-2; i >= 0; --i) {
235 path.lineTo(points[i]);
236 }
237 }
238
239 path.close();
240#ifdef SK_DEBUG
241 // Each path this method returns should be convex, only composed of
242 // lines, wound the right direction, and short enough to fit in one
243 // of the GMs rows.
244 SkASSERT(path.isConvex());
245 SkASSERT(SkPath::kLine_SegmentMask == path.getSegmentMasks());
reed026beb52015-06-10 14:23:15 -0700246 SkPathPriv::FirstDirection actualDir;
247 SkASSERT(SkPathPriv::CheapComputeFirstDirection(path, &actualDir));
248 SkASSERT(SkPathPriv::AsFirstDirection(dir) == actualDir);
robertphillips72729352015-04-28 07:42:04 -0700249 SkRect bounds = path.getBounds();
250 SkASSERT(SkScalarNearlyEqual(bounds.centerX(), 0.0f));
251 SkASSERT(bounds.height() <= kMaxPathHeight);
252#endif
253 return path;
254 }
255
256 // Draw a single path several times, shrinking it, flipping its direction
257 // and changing its start vertex each time.
robertphillips2a974622015-05-08 07:08:13 -0700258 void drawPath(SkCanvas* canvas, int index, SkPoint* offset) {
robertphillips72729352015-04-28 07:42:04 -0700259
260 SkPoint center;
261 {
262 SkPath path = GetPath(index, 0, SkPath::kCW_Direction);
robertphillips2a974622015-05-08 07:08:13 -0700263 if (offset->fX+path.getBounds().width() > kGMWidth) {
264 offset->fX = 0;
265 offset->fY += kMaxPathHeight;
robertphillipsad234462016-08-26 05:30:19 -0700266 if (fDoStrokeAndFill) {
267 offset->fX += kStrokeWidth / 2.0f;
268 offset->fY += kStrokeWidth / 2.0f;
269 }
robertphillips72729352015-04-28 07:42:04 -0700270 }
robertphillips2a974622015-05-08 07:08:13 -0700271 center = { offset->fX + SkScalarHalf(path.getBounds().width()), offset->fY};
272 offset->fX += path.getBounds().width();
robertphillipsad234462016-08-26 05:30:19 -0700273 if (fDoStrokeAndFill) {
274 offset->fX += kStrokeWidth;
275 }
robertphillips72729352015-04-28 07:42:04 -0700276 }
277
278 const SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
279 const SkPath::Direction dirs[2] = { SkPath::kCW_Direction, SkPath::kCCW_Direction };
280 const float scales[] = { 1.0f, 0.75f, 0.5f, 0.25f, 0.1f, 0.01f, 0.001f };
mtkleindbfd7ab2016-09-01 11:24:54 -0700281 const SkPaint::Join joins[3] = { SkPaint::kRound_Join,
robertphillipsad234462016-08-26 05:30:19 -0700282 SkPaint::kBevel_Join,
283 SkPaint::kMiter_Join };
robertphillips72729352015-04-28 07:42:04 -0700284
285 SkPaint paint;
286 paint.setAntiAlias(true);
287
288 for (size_t i = 0; i < SK_ARRAY_COUNT(scales); ++i) {
289 SkPath path = GetPath(index, (int) i, dirs[i%2]);
robertphillipsad234462016-08-26 05:30:19 -0700290 if (fDoStrokeAndFill) {
291 paint.setStyle(SkPaint::kStrokeAndFill_Style);
292 paint.setStrokeJoin(joins[i%3]);
293 paint.setStrokeWidth(SkIntToScalar(kStrokeWidth));
294 }
robertphillips72729352015-04-28 07:42:04 -0700295
296 canvas->save();
297 canvas->translate(center.fX, center.fY);
298 canvas->scale(scales[i], scales[i]);
299 paint.setColor(colors[i%2]);
300 canvas->drawPath(path, paint);
301 canvas->restore();
302 }
303 }
304
305 void onDraw(SkCanvas* canvas) override {
robertphillips2a974622015-05-08 07:08:13 -0700306 // the right edge of the last drawn path
halcanary9d524f22016-03-29 09:03:52 -0700307 SkPoint offset = { 0, SkScalarHalf(kMaxPathHeight) };
robertphillipsad234462016-08-26 05:30:19 -0700308 if (fDoStrokeAndFill) {
309 offset.fX += kStrokeWidth / 2.0f;
310 offset.fY += kStrokeWidth / 2.0f;
311 }
robertphillips2a974622015-05-08 07:08:13 -0700312
robertphillips72729352015-04-28 07:42:04 -0700313 for (int i = 0; i < kNumPaths; ++i) {
robertphillips2a974622015-05-08 07:08:13 -0700314 this->drawPath(canvas, i, &offset);
robertphillips72729352015-04-28 07:42:04 -0700315 }
316
317 // Repro for crbug.com/472723 (Missing AA on portions of graphic with GPU rasterization)
318 {
319 canvas->translate(356.0f, 50.0f);
320
321 SkPaint p;
322 p.setAntiAlias(true);
robertphillipsad234462016-08-26 05:30:19 -0700323 if (fDoStrokeAndFill) {
324 p.setStyle(SkPaint::kStrokeAndFill_Style);
325 p.setStrokeJoin(SkPaint::kMiter_Join);
326 p.setStrokeWidth(SkIntToScalar(kStrokeWidth));
327 }
robertphillips72729352015-04-28 07:42:04 -0700328
329 SkPath p1;
330 p1.moveTo(60.8522949f, 364.671021f);
331 p1.lineTo(59.4380493f, 364.671021f);
332 p1.lineTo(385.414276f, 690.647217f);
333 p1.lineTo(386.121399f, 689.940125f);
334 canvas->drawPath(p1, p);
335 }
336 }
337
338private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700339 static constexpr int kStrokeWidth = 10;
340 static constexpr int kNumPaths = 20;
341 static constexpr int kMaxPathHeight = 100;
342 static constexpr int kGMWidth = 512;
343 static constexpr int kGMHeight = 512;
robertphillips72729352015-04-28 07:42:04 -0700344
robertphillipsad234462016-08-26 05:30:19 -0700345 bool fDoStrokeAndFill;
346
robertphillips72729352015-04-28 07:42:04 -0700347 typedef GM INHERITED;
348};
349
350//////////////////////////////////////////////////////////////////////////////
351
robertphillipsad234462016-08-26 05:30:19 -0700352DEF_GM(return new ConvexLineOnlyPathsGM(false);)
353DEF_GM(return new ConvexLineOnlyPathsGM(true);)
robertphillips72729352015-04-28 07:42:04 -0700354}