• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
9 #include "SkPolyUtils.h"
10 #include "SkPathPriv.h"
11 
create_ngon(int n,SkPoint * pts,SkScalar width,SkScalar height)12 static void create_ngon(int n, SkPoint* pts, SkScalar width, SkScalar height) {
13     float angleStep = 360.0f / n, angle = 0.0f, sin, cos;
14     if ((n % 2) == 1) {
15         angle = angleStep/2.0f;
16     }
17 
18     for (int i = 0; i < n; ++i) {
19         sin = SkScalarSinCos(SkDegreesToRadians(angle), &cos);
20         pts[i].fX = -sin * width;
21         pts[i].fY = cos * height;
22         angle += angleStep;
23     }
24 }
25 
26 namespace ConvexLineOnlyData {
27 // narrow rect
28 const SkPoint gPoints0[] = {
29     { -1.5f, -50.0f },
30     { 1.5f, -50.0f },
31     { 1.5f,  50.0f },
32     { -1.5f,  50.0f }
33 };
34 // narrow rect on an angle
35 const SkPoint gPoints1[] = {
36     { -50.0f, -49.0f },
37     { -49.0f, -50.0f },
38     { 50.0f,  49.0f },
39     { 49.0f,  50.0f }
40 };
41 // trap - narrow on top - wide on bottom
42 const SkPoint gPoints2[] = {
43     { -10.0f, -50.0f },
44     { 10.0f, -50.0f },
45     { 50.0f,  50.0f },
46     { -50.0f,  50.0f }
47 };
48 // wide skewed rect
49 const SkPoint gPoints3[] = {
50     { -50.0f, -50.0f },
51     { 0.0f, -50.0f },
52     { 50.0f,  50.0f },
53     { 0.0f,  50.0f }
54 };
55 // thin rect with colinear-ish lines
56 const SkPoint gPoints4[] = {
57     { -6.0f, -50.0f },
58     { 4.0f, -50.0f },
59 #if SK_TREAT_COLINEAR_DIAGONAL_POINTS_AS_CONCAVE == 0
60     { 5.0f, -25.0f },  // remove if collinear diagonal points are not concave
61 #endif
62     { 6.0f,   0.0f },
63 #if SK_TREAT_COLINEAR_DIAGONAL_POINTS_AS_CONCAVE == 0
64     { 5.0f,  25.0f },  // remove if collinear diagonal points are not concave
65 #endif
66     { 4.0f,  50.0f },
67     { -4.0f,  50.0f }
68 };
69 // degenerate
70 const SkPoint gPoints5[] = {
71     { -0.025f, -0.025f },
72     { 0.025f, -0.025f },
73     { 0.025f,  0.025f },
74     { -0.025f,  0.025f }
75 };
76 // Triangle in which the first point should fuse with last
77 const SkPoint gPoints6[] = {
78     { -20.0f, -13.0f },
79     { -20.0f, -13.05f },
80     { 20.0f, -13.0f },
81     { 20.0f,  27.0f }
82 };
83 // thin rect with colinear lines
84 const SkPoint gPoints7[] = {
85     { -10.0f, -50.0f },
86     { 10.0f, -50.0f },
87     { 10.0f, -25.0f },
88     { 10.0f,   0.0f },
89     { 10.0f,  25.0f },
90     { 10.0f,  50.0f },
91     { -10.0f,  50.0f }
92 };
93 // capped teardrop
94 const SkPoint gPoints8[] = {
95     { 50.00f,  50.00f },
96     { 0.00f,  50.00f },
97     { -15.45f,  47.55f },
98     { -29.39f,  40.45f },
99     { -40.45f,  29.39f },
100     { -47.55f,  15.45f },
101     { -50.00f,   0.00f },
102     { -47.55f, -15.45f },
103     { -40.45f, -29.39f },
104     { -29.39f, -40.45f },
105     { -15.45f, -47.55f },
106     { 0.00f, -50.00f },
107     { 50.00f, -50.00f }
108 };
109 // teardrop
110 const SkPoint gPoints9[] = {
111     { 4.39f,  40.45f },
112     { -9.55f,  47.55f },
113     { -25.00f,  50.00f },
114     { -40.45f,  47.55f },
115     { -54.39f,  40.45f },
116     { -65.45f,  29.39f },
117     { -72.55f,  15.45f },
118     { -75.00f,   0.00f },
119     { -72.55f, -15.45f },
120     { -65.45f, -29.39f },
121     { -54.39f, -40.45f },
122     { -40.45f, -47.55f },
123     { -25.0f,  -50.0f },
124     { -9.55f, -47.55f },
125     { 4.39f, -40.45f },
126     { 75.00f,   0.00f }
127 };
128 // clipped triangle
129 const SkPoint gPoints10[] = {
130     { -10.0f, -50.0f },
131     { 10.0f, -50.0f },
132     { 50.0f,  31.0f },
133     { 40.0f,  50.0f },
134     { -40.0f,  50.0f },
135     { -50.0f,  31.0f },
136 };
137 
138 const SkPoint* gPoints[] = {
139     gPoints0, gPoints1, gPoints2, gPoints3, gPoints4, gPoints5, gPoints6,
140     gPoints7, gPoints8, gPoints9, gPoints10,
141 };
142 
143 const size_t gSizes[] = {
144     SK_ARRAY_COUNT(gPoints0),
145     SK_ARRAY_COUNT(gPoints1),
146     SK_ARRAY_COUNT(gPoints2),
147     SK_ARRAY_COUNT(gPoints3),
148     SK_ARRAY_COUNT(gPoints4),
149     SK_ARRAY_COUNT(gPoints5),
150     SK_ARRAY_COUNT(gPoints6),
151     SK_ARRAY_COUNT(gPoints7),
152     SK_ARRAY_COUNT(gPoints8),
153     SK_ARRAY_COUNT(gPoints9),
154     SK_ARRAY_COUNT(gPoints10),
155 };
156 static_assert(SK_ARRAY_COUNT(gSizes) == SK_ARRAY_COUNT(gPoints), "array_mismatch");
157 }
158 
159 namespace skiagm {
160 
161 // This GM is intended to exercise Ganesh's handling of convex line-only
162 // paths
163 class ConvexLineOnlyPathsGM : public GM {
164 public:
ConvexLineOnlyPathsGM(bool doStrokeAndFill)165     ConvexLineOnlyPathsGM(bool doStrokeAndFill) : fDoStrokeAndFill(doStrokeAndFill) {
166         this->setBGColor(0xFFFFFFFF);
167     }
168 
169 protected:
onShortName()170     SkString onShortName() override {
171         if (fDoStrokeAndFill) {
172             return SkString("convex-lineonly-paths-stroke-and-fill");
173         }
174         return SkString("convex-lineonly-paths");
175     }
onISize()176     SkISize onISize() override { return SkISize::Make(kGMWidth, kGMHeight); }
runAsBench() const177     bool runAsBench() const override { return true; }
178 
GetPath(int index,SkPath::Direction dir)179     static SkPath GetPath(int index, SkPath::Direction dir) {
180         std::unique_ptr<SkPoint[]> data(nullptr);
181         const SkPoint* points;
182         int numPts;
183         if (index < (int) SK_ARRAY_COUNT(ConvexLineOnlyData::gPoints)) {
184             // manually specified
185             points = ConvexLineOnlyData::gPoints[index];
186             numPts = (int)ConvexLineOnlyData::gSizes[index];
187         } else {
188             // procedurally generated
189             SkScalar width = kMaxPathHeight/2;
190             SkScalar height = kMaxPathHeight/2;
191             switch (index-SK_ARRAY_COUNT(ConvexLineOnlyData::gPoints)) {
192             case 0:
193                 numPts = 3;
194                 break;
195             case 1:
196                 numPts = 4;
197                 break;
198             case 2:
199                 numPts = 5;
200                 break;
201             case 3:             // squashed pentagon
202                 numPts = 5;
203                 width = kMaxPathHeight/5;
204                 break;
205             case 4:
206                 numPts = 6;
207                 break;
208             case 5:
209                 numPts = 8;
210                 break;
211             case 6:              // squashed octogon
212                 numPts = 8;
213                 width = kMaxPathHeight/5;
214                 break;
215             case 7:
216                 numPts = 20;
217                 break;
218             case 8:
219                 numPts = 100;
220                 break;
221             default:
222                 numPts = 3;
223                 break;
224             }
225 
226             data.reset(new SkPoint[numPts]);
227 
228             create_ngon(numPts, data.get(), width, height);
229             points = data.get();
230         }
231 
232         SkPath path;
233 
234         if (SkPath::kCW_Direction == dir) {
235             path.moveTo(points[0]);
236             for (int i = 1; i < numPts; ++i) {
237                 path.lineTo(points[i]);
238             }
239         } else {
240             path.moveTo(points[numPts-1]);
241             for (int i = numPts-2; i >= 0; --i) {
242                 path.lineTo(points[i]);
243             }
244         }
245 
246         path.close();
247 #ifdef SK_DEBUG
248         // Each path this method returns should be convex, only composed of
249         // lines, wound the right direction, and short enough to fit in one
250         // of the GMs rows.
251         SkASSERT(path.isConvex());
252         SkASSERT(SkPath::kLine_SegmentMask == path.getSegmentMasks());
253         SkPathPriv::FirstDirection actualDir;
254         SkASSERT(SkPathPriv::CheapComputeFirstDirection(path, &actualDir));
255         SkASSERT(SkPathPriv::AsFirstDirection(dir) == actualDir);
256         SkRect bounds = path.getBounds();
257         SkASSERT(SkScalarNearlyEqual(bounds.centerX(), 0.0f));
258         SkASSERT(bounds.height() <= kMaxPathHeight);
259 #endif
260         return path;
261     }
262 
263     // Draw a single path several times, shrinking it, flipping its direction
264     // and changing its start vertex each time.
drawPath(SkCanvas * canvas,int index,SkPoint * offset)265     void drawPath(SkCanvas* canvas, int index, SkPoint* offset) {
266 
267         SkPoint center;
268         {
269             SkPath path = GetPath(index, SkPath::kCW_Direction);
270             if (offset->fX+path.getBounds().width() > kGMWidth) {
271                 offset->fX = 0;
272                 offset->fY += kMaxPathHeight;
273                 if (fDoStrokeAndFill) {
274                     offset->fX += kStrokeWidth / 2.0f;
275                     offset->fY += kStrokeWidth / 2.0f;
276                 }
277             }
278             center = { offset->fX + SkScalarHalf(path.getBounds().width()), offset->fY};
279             offset->fX += path.getBounds().width();
280             if (fDoStrokeAndFill) {
281                 offset->fX += kStrokeWidth;
282             }
283         }
284 
285         const SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
286         const SkPath::Direction dirs[2] = { SkPath::kCW_Direction, SkPath::kCCW_Direction };
287         const float scales[] = { 1.0f, 0.75f, 0.5f, 0.25f, 0.1f, 0.01f, 0.001f };
288         const SkPaint::Join joins[3] = { SkPaint::kRound_Join,
289                                          SkPaint::kBevel_Join,
290                                          SkPaint::kMiter_Join };
291 
292         SkPaint paint;
293         paint.setAntiAlias(true);
294 
295         for (size_t i = 0; i < SK_ARRAY_COUNT(scales); ++i) {
296             SkPath path = GetPath(index, dirs[i%2]);
297             if (fDoStrokeAndFill) {
298                 paint.setStyle(SkPaint::kStrokeAndFill_Style);
299                 paint.setStrokeJoin(joins[i%3]);
300                 paint.setStrokeWidth(SkIntToScalar(kStrokeWidth));
301             }
302 
303             canvas->save();
304                 canvas->translate(center.fX, center.fY);
305                 canvas->scale(scales[i], scales[i]);
306                 paint.setColor(colors[i%2]);
307                 canvas->drawPath(path, paint);
308             canvas->restore();
309         }
310     }
311 
onDraw(SkCanvas * canvas)312     void onDraw(SkCanvas* canvas) override {
313         // the right edge of the last drawn path
314         SkPoint offset = { 0, SkScalarHalf(kMaxPathHeight) };
315         if (fDoStrokeAndFill) {
316             offset.fX += kStrokeWidth / 2.0f;
317             offset.fY += kStrokeWidth / 2.0f;
318         }
319 
320         for (int i = 0; i < kNumPaths; ++i) {
321             this->drawPath(canvas, i, &offset);
322         }
323 
324         {
325             // Repro for crbug.com/472723 (Missing AA on portions of graphic with GPU rasterization)
326 
327             SkPaint p;
328             p.setAntiAlias(true);
329             if (fDoStrokeAndFill) {
330                 p.setStyle(SkPaint::kStrokeAndFill_Style);
331                 p.setStrokeJoin(SkPaint::kMiter_Join);
332                 p.setStrokeWidth(SkIntToScalar(kStrokeWidth));
333             }
334 
335             SkPath p1;
336             p1.moveTo(60.8522949f, 364.671021f);
337             p1.lineTo(59.4380493f, 364.671021f);
338             p1.lineTo(385.414276f, 690.647217f);
339             p1.lineTo(386.121399f, 689.940125f);
340             canvas->save();
341             canvas->translate(356.0f, 50.0f);
342             canvas->drawPath(p1, p);
343             canvas->restore();
344 
345             // Repro for crbug.com/869172 (SVG path incorrectly simplified when using GPU
346             // Rasterization). This will only draw anything in the stroke-and-fill version.
347             SkPath p2;
348             p2.moveTo(10.f, 0.f);
349             p2.lineTo(38.f, 0.f);
350             p2.lineTo(66.f, 0.f);
351             p2.lineTo(94.f, 0.f);
352             p2.lineTo(122.f, 0.f);
353             p2.lineTo(150.f, 0.f);
354             p2.lineTo(150.f, 0.f);
355             p2.lineTo(122.f, 0.f);
356             p2.lineTo(94.f, 0.f);
357             p2.lineTo(66.f, 0.f);
358             p2.lineTo(38.f, 0.f);
359             p2.lineTo(10.f, 0.f);
360             p2.close();
361             canvas->save();
362             canvas->translate(0.0f, 500.0f);
363             canvas->drawPath(p2, p);
364             canvas->restore();
365 
366             // Repro for crbug.com/856137. This path previously caused GrAAConvexTessellator to turn
367             // inset rings into outsets when adjacent bisector angles converged outside the previous
368             // ring due to accumulated error.
369             SkPath p3;
370             p3.setFillType(SkPath::kEvenOdd_FillType);
371             p3.moveTo(1184.96f, 982.557f);
372             p3.lineTo(1183.71f, 982.865f);
373             p3.lineTo(1180.99f, 982.734f);
374             p3.lineTo(1178.5f, 981.541f);
375             p3.lineTo(1176.35f, 979.367f);
376             p3.lineTo(1178.94f, 938.854f);
377             p3.lineTo(1181.35f, 936.038f);
378             p3.lineTo(1183.96f, 934.117f);
379             p3.lineTo(1186.67f, 933.195f);
380             p3.lineTo(1189.36f, 933.342f);
381             p3.lineTo(1191.58f, 934.38f);
382             p3.close();
383             canvas->save();
384             SkMatrix m;
385             m.setAll(0.0893210843f, 0, 79.1197586f, 0, 0.0893210843f, 300, 0, 0, 1);
386             canvas->concat(m);
387             canvas->drawPath(p3, p);
388             canvas->restore();
389         }
390     }
391 
392 private:
393     static constexpr int kStrokeWidth   = 10;
394     static constexpr int kNumPaths      = 20;
395     static constexpr int kMaxPathHeight = 100;
396     static constexpr int kGMWidth       = 512;
397     static constexpr int kGMHeight      = 512;
398 
399     bool fDoStrokeAndFill;
400 
401     typedef GM INHERITED;
402 };
403 
404 //////////////////////////////////////////////////////////////////////////////
405 
406 DEF_GM(return new ConvexLineOnlyPathsGM(false);)
407 DEF_GM(return new ConvexLineOnlyPathsGM(true);)
408 }
409