Debian Codegraph @

gm/bug12866.cpp

raw
1 /*
2 * Copyright 2022 Google LLC
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/SkPath.h"
11 #include "include/core/SkPathBuilder.h"
12 #include "include/core/SkPathUtils.h"
13 #include "src/base/SkFloatBits.h"
14
15 static SkPath get_path() {
16 SkPathBuilder builder(SkPathFillType::kWinding);
17 builder.moveTo(SkBits2Float(0x45034ec4), SkBits2Float(0x42e7fb80)); // 2100.92f, 115.991f
18 builder.quadTo(SkBits2Float(0x4500f46c),
19 SkBits2Float(0x43333300),
20 SkBits2Float(0x4500f46c),
21 SkBits2Float(0x431f0ec0)); // 2063.28f, 179.199f, 2063.28f, 159.058f
22 builder.quadTo(SkBits2Float(0x4500f46c),
23 SkBits2Float(0x430ad7c0),
24 SkBits2Float(0x45019462),
25 SkBits2Float(0x42fed580)); // 2063.28f, 138.843f, 2073.27f, 127.417f
26 builder.quadTo(SkBits2Float(0x45023458),
27 SkBits2Float(0x42e7fb80),
28 SkBits2Float(0x45034ec4),
29 SkBits2Float(0x42e7fb80)); // 2083.27f, 115.991f, 2100.92f, 115.991f
30 builder.close();
31 return builder.detach();
32 }
33
34 // Reproduces the underlying problem from skbug.com/40043963.
35 // The path (part of a glyph) was being drawn stroked, and with a perspective matrix.
36 // The perspective matrix forces a very large resScale when stroking the path.
37 // The resulting filled path is incorrect. Note that stroking with a smaller resScale works fine.
38 DEF_SIMPLE_GM(bug12866, canvas, 128, 64) {
39 SkPaint strokePaint;
40 strokePaint.setAntiAlias(true);
41 strokePaint.setStyle(SkPaint::kStroke_Style);
42 strokePaint.setStrokeWidth(3);
43
44 SkPaint fillPaint;
45 fillPaint.setAntiAlias(true);
46
47 SkPath strokePath = get_path();
48 SkPathBuilder builder;
49 skpathutils::FillPathWithPaint(strokePath, strokePaint, &builder, nullptr,
50 SkMatrix::Scale(1200.0f, 1200.0f));
51 SkPath fillPath = builder.detach();
52
53 SkRect strokeBounds = strokePath.getBounds();
54 SkRect fillBounds = fillPath.getBounds();
55
56 // Draw the stroked path. This (internally) uses a resScale of 1.0, and looks good.
57 canvas->save();
58 canvas->translate(10 - strokeBounds.fLeft, 10 - strokeBounds.fTop);
59 canvas->drawPath(strokePath, strokePaint);
60 canvas->restore();
61
62 // With a perspective CTM, it's possible for resScale to become large. Draw the filled
63 // path produced by the stroker in that situation, which ends up being incorrect.
64 canvas->save();
65 canvas->translate(74 - fillBounds.fLeft, 10 - fillBounds.fTop);
66 canvas->drawPath(fillPath, fillPaint);
67 canvas->restore();
68 }
69
70 // This is another example of the same underlying bug (recursion limit in the stroker),
71 // but with cubics, rather than quads.
72 DEF_SIMPLE_GM(bug40810065, canvas, 256, 512) {
73 canvas->scale(2.f, 2.f);
74
75 SkPathBuilder path1;
76 path1.moveTo(108.87f, 3.78f);
77 path1.cubicTo(201.1f, -128.61f, 34.21f, 82.54f, 134.14f, 126.01f);
78 SkPathBuilder path2;
79 path2.moveTo(108.87f, 3.78f);
80 path2.cubicTo(201.f, -128.61f, 34.21f, 82.54f, 134.14f, 126.f);
81
82 SkPaint stroke;
83 stroke.setColor(SK_ColorBLACK);
84 stroke.setAntiAlias(true);
85 stroke.setStyle(SkPaint::kStroke_Style);
86 stroke.setStrokeWidth(1.f);
87 stroke.setStrokeCap(SkPaint::kRound_Cap);
88
89 canvas->save();
90 canvas->translate(-75.f, 50.f);
91 canvas->drawPath(path1.detach(), stroke);
92 canvas->restore();
93
94 canvas->save();
95 canvas->translate(-20.f, 100.f);
96 canvas->drawPath(path2.detach(), stroke);
97 canvas->restore();
98 }
99
100 // Finally: A repro case that involves conics. This should draw NOTHING. When incorrect, it drew
101 // a large black rectangle over half of the slide.
102 DEF_SIMPLE_GM_BG(bug41422450, canvas, 863, 473, SK_ColorWHITE) {
103 SkM44 mat{1, -0.00000139566271f, 0, -2321738,
104 0.000113059919f, 0.0123444516f, 0, -353,
105 0, 0, 1, 0,
106 0, 0, 0, 1};
107 canvas->concat(mat);
108
109 SkRect circle = SkRect::MakeLTRB(-3299135.5f, -12312541.0f, 9897407.0f, 884000.812f);
110
111 SkPath strokePath = SkPathBuilder().arcTo(circle, 59.9999962f, 59.9999962f, true).detach();
112
113 SkPaint strokePaint;
114 strokePaint.setStyle(SkPaint::kStroke_Style);
115 strokePaint.setStrokeWidth(2);
116 canvas->drawPath(strokePath, strokePaint);
117 }