1 /*
2 * Copyright 2014 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/SkPaint.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/SkFloatBits.h"
15 #include "include/private/SkPathRef.h"
16 #include "src/core/SkPathPriv.h"
17
18 #define W 800
19 #define H 800
20
DEF_SIMPLE_GM(analytic_antialias_convex,canvas,W,H)21 DEF_SIMPLE_GM(analytic_antialias_convex, canvas, W, H) {
22 SkPaint p;
23 p.setColor(SK_ColorRED);
24 p.setAntiAlias(true);
25
26 canvas->clear(0xFFFFFFFF);
27
28 canvas->save();
29
30 SkScalar y = 0;
31
32 canvas->translate(0, y);
33 canvas->rotate(1);
34 canvas->drawRect({ 20, 20, 200, 200 }, p);
35 canvas->restore();
36
37 y += 200;
38
39 canvas->save();
40 canvas->translate(0, y);
41 canvas->rotate(1);
42 canvas->drawRect({ 20, 20, 20.2f, 200 }, p);
43 canvas->drawRect({ 20, 200, 200, 200.1f }, p);
44 canvas->drawCircle(100, 100, 30, p);
45 canvas->restore();
46
47 // The following path is empty but it'll reveal bug chrome:662914
48 SkPathBuilder path;
49 path.moveTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f
50 // 77.8075f, 231.626f, 77.8074f, 231.625f, 77.8073f, 231.625f
51 path.cubicTo(SkBits2Float(0x429b9d71), SkBits2Float(0x4367a022),
52 SkBits2Float(0x429b9d64), SkBits2Float(0x4367a009),
53 SkBits2Float(0x429b9d50), SkBits2Float(0x43679ff2));
54 path.lineTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f
55 path.close();
56 canvas->drawPath(path.detach(), p);
57
58 // The following path reveals a subtle SkAnalyticQuadraticEdge::updateQuadratic bug:
59 // we should not use any snapped y for the intermediate values whose error may accumulate;
60 // snapping should only be allowed once before updateLine.
61 path.moveTo(SkBits2Float(0x434ba71e), SkBits2Float(0x438a06d0)); // 203.653f, 276.053f
62 path.lineTo(SkBits2Float(0x43492a74), SkBits2Float(0x4396d70d)); // 201.166f, 301.68f
63 // 200.921f, 304.207f, 196.939f, 303.82f, 0.707107f
64 path.conicTo(SkBits2Float(0x4348ebaf), SkBits2Float(0x43981a75),
65 SkBits2Float(0x4344f079), SkBits2Float(0x4397e900), SkBits2Float(0x3f3504f3));
66 path.close();
67 // Manually setting convexity is required. Otherwise, this path will be considered concave.
68 SkPathPriv::SetConvexity(&path, SkPathConvexity::kConvex);
69 canvas->drawPath(path.detach(), p);
70
71 // skbug.com/7573
72 y += 200;
73 canvas->save();
74 canvas->translate(0, y);
75 p.setAntiAlias(true);
76 path.moveTo(1.98009784f, 9.0162744f);
77 path.lineTo(47.843992f, 10.1922744f);
78 path.lineTo(47.804008f, 11.7597256f);
79 path.lineTo(1.93990216f, 10.5837256f);
80 canvas->drawPath(path.detach(), p);
81 canvas->restore();
82
83 // skbug.com/7813
84 // t8888 splits the 800-high canvas into 3 pieces; the boundary is close to 266 and 534
85 path.moveTo(700, 266);
86 path.lineTo(710, 266);
87 path.lineTo(710, 534);
88 path.lineTo(700, 534);
89 canvas->drawPath(path.detach(), p);
90 }
91
DEF_SIMPLE_GM(analytic_antialias_general,canvas,W,H)92 DEF_SIMPLE_GM(analytic_antialias_general, canvas, W, H) {
93 SkPaint p;
94 p.setColor(SK_ColorRED);
95 p.setAntiAlias(true);
96
97 canvas->clear(0xFFFFFFFF);
98
99 canvas->save();
100 canvas->rotate(1);
101 const SkScalar R = 115.2f, C = 128.0f;
102 SkPathBuilder builder;
103 builder.moveTo(C + R, C);
104 for (int i = 1; i < 8; ++i) {
105 SkScalar a = 2.6927937f * i;
106 builder.lineTo(C + R * SkScalarCos(a), C + R * SkScalarSin(a));
107 }
108 SkPath path = builder.detach();
109 canvas->drawPath(path, p);
110 canvas->restore();
111
112 canvas->save();
113 canvas->translate(200, 0);
114 canvas->rotate(1);
115 p.setStyle(SkPaint::kStroke_Style);
116 p.setStrokeWidth(5);
117 canvas->drawPath(path, p);
118 canvas->restore();
119
120
121 // The following two paths test if we correctly cumulates the alpha on the middle pixel
122 // column where the left rect and the right rect abut.
123 p.setStyle(SkPaint::kFill_Style);
124 canvas->translate(0, 300);
125 canvas->drawPath(SkPathBuilder().addRect({20, 20, 100.4999f, 100})
126 .addRect({100.5001f, 20, 200, 100})
127 .detach(), p);
128
129 canvas->translate(300, 0);
130 canvas->drawPath(SkPathBuilder().addRect({20, 20, 100.1f, 100})
131 .addRect({100.9f, 20, 200, 100})
132 .detach(), p);
133 }
134
DEF_SIMPLE_GM(analytic_antialias_inverse,canvas,W,H)135 DEF_SIMPLE_GM(analytic_antialias_inverse, canvas, W, H) {
136 SkPaint p;
137 p.setColor(SK_ColorRED);
138 p.setAntiAlias(true);
139
140 canvas->save();
141
142 SkPath path = SkPath::Circle(100, 100, 30);
143 path.setFillType(SkPathFillType::kInverseWinding);
144 canvas->drawPath(path, p);
145 canvas->restore();
146 }
147