blob: b2fc6337d964efdbd8809a4f3dfb8e1459cd00a5 [file] [log] [blame]
liyuqiane60d8552016-10-03 13:49:37 -07001/*
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.h"
9#include "SkPath.h"
10#include "SkScan.h"
11
12#define W 800
13#define H 800
14
15class AnalyticAntiAliasConvexGM : public skiagm::GM {
16public:
17 AnalyticAntiAliasConvexGM() {}
18
19protected:
20
21 SkString onShortName() override {
22 return SkString("analytic_antialias_convex");
23 }
24
25 SkISize onISize() override {
26 return SkISize::Make(W, H);
27 }
28
29 void onDraw(SkCanvas* canvas) override {
30 SkPaint p;
31 p.setColor(SK_ColorRED);
32 p.setAntiAlias(true);
33
34 canvas->clear(0xFFFFFFFF);
35
36 canvas->save();
37
38 SkScalar y = 0;
39
40 canvas->translate(0, y);
41 canvas->rotate(1);
Mike Reed3661bc92017-02-22 13:21:42 -050042 canvas->drawRect({ 20, 20, 200, 200 }, p);
liyuqiane60d8552016-10-03 13:49:37 -070043 canvas->restore();
44
45 y += 200;
46
Yuqian Li63f69cd2018-04-18 15:09:44 -040047 canvas->save();
liyuqiane60d8552016-10-03 13:49:37 -070048 canvas->translate(0, y);
49 canvas->rotate(1);
Mike Reed3661bc92017-02-22 13:21:42 -050050 canvas->drawRect({ 20, 20, 20.2f, 200 }, p);
51 canvas->drawRect({ 20, 200, 200, 200.1f }, p);
liyuqiane60d8552016-10-03 13:49:37 -070052 canvas->drawCircle(100, 100, 30, p);
53 canvas->restore();
liyuqian451ceba2016-11-07 08:10:44 -080054
55 // The following path is empty but it'll reveal bug chrome:662914
56 SkPath path;
57 path.moveTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f
58 // 77.8075f, 231.626f, 77.8074f, 231.625f, 77.8073f, 231.625f
59 path.cubicTo(SkBits2Float(0x429b9d71), SkBits2Float(0x4367a022),
60 SkBits2Float(0x429b9d64), SkBits2Float(0x4367a009),
61 SkBits2Float(0x429b9d50), SkBits2Float(0x43679ff2));
62 path.lineTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f
63 path.close();
64 canvas->drawPath(path, p);
Yuqian Li0038b7f2016-11-14 11:51:41 -050065
66 // The following path reveals a subtle SkAnalyticQuadraticEdge::updateQuadratic bug:
67 // we should not use any snapped y for the intermediate values whose error may accumulate;
68 // snapping should only be allowed once before updateLine.
69 path.reset();
70 path.moveTo(SkBits2Float(0x434ba71e), SkBits2Float(0x438a06d0)); // 203.653f, 276.053f
71 path.lineTo(SkBits2Float(0x43492a74), SkBits2Float(0x4396d70d)); // 201.166f, 301.68f
72 // 200.921f, 304.207f, 196.939f, 303.82f, 0.707107f
73 path.conicTo(SkBits2Float(0x4348ebaf), SkBits2Float(0x43981a75),
74 SkBits2Float(0x4344f079), SkBits2Float(0x4397e900), SkBits2Float(0x3f3504f3));
75 path.close();
76 // Manually setting convexity is required. Otherwise, this path will be considered concave.
77 path.setConvexity(SkPath::kConvex_Convexity);
78 canvas->drawPath(path, p);
Yuqian Li4a657ad2018-02-07 19:04:34 +080079
80 // skbug.com/7573
81 y += 200;
Yuqian Li63f69cd2018-04-18 15:09:44 -040082 canvas->save();
Yuqian Li4a657ad2018-02-07 19:04:34 +080083 canvas->translate(0, y);
84 p.setAntiAlias(true);
85 path.reset();
86 path.moveTo(1.98009784f, 9.0162744f);
87 path.lineTo(47.843992f, 10.1922744f);
88 path.lineTo(47.804008f, 11.7597256f);
89 path.lineTo(1.93990216f, 10.5837256f);
90 canvas->drawPath(path, p);
91 canvas->restore();
Yuqian Li63f69cd2018-04-18 15:09:44 -040092
93 // skbug.com/7813
94 // t8888 splits the 800-high canvas into 3 pieces; the boundary is close to 266 and 534
95 path.reset();
96 path.moveTo(700, 266);
97 path.lineTo(710, 266);
98 path.lineTo(710, 534);
99 path.lineTo(700, 534);
100 canvas->drawPath(path, p);
liyuqiane60d8552016-10-03 13:49:37 -0700101 }
102
103private:
104 typedef skiagm::GM INHERITED;
105};
106
Yuqian Li131c1fb2016-12-12 16:24:47 -0500107class AnalyticAntiAliasGeneralGM : public skiagm::GM {
108public:
109 AnalyticAntiAliasGeneralGM() {}
110
111protected:
112
113 SkString onShortName() override {
114 return SkString("analytic_antialias_general");
115 }
116
117 SkISize onISize() override {
118 return SkISize::Make(W, H);
119 }
120
121 void onDraw(SkCanvas* canvas) override {
122 SkPaint p;
123 p.setColor(SK_ColorRED);
124 p.setAntiAlias(true);
125
126 canvas->clear(0xFFFFFFFF);
127
128 canvas->save();
129 canvas->rotate(1);
130 const SkScalar R = 115.2f, C = 128.0f;
131 SkPath path;
132 path.moveTo(C + R, C);
133 for (int i = 1; i < 8; ++i) {
134 SkScalar a = 2.6927937f * i;
135 SkScalar cosine;
136 SkScalar sine = SkScalarSinCos(a, &cosine);
137 path.lineTo(C + R * cosine, C + R * sine);
138 }
139 canvas->drawPath(path, p);
140 canvas->restore();
141
142 canvas->save();
143 canvas->translate(200, 0);
144 canvas->rotate(1);
145 p.setStyle(SkPaint::kStroke_Style);
146 p.setStrokeWidth(5);
147 canvas->drawPath(path, p);
148 canvas->restore();
Yuqian Lifbe07932017-11-07 11:19:52 -0500149
150
151 // The following two paths test if we correctly cumulates the alpha on the middle pixel
152 // column where the left rect and the right rect abut.
153 p.setStyle(SkPaint::kFill_Style);
154 canvas->translate(0, 300);
155 path.reset();
156 path.addRect({20, 20, 100.4999f, 100});
157 path.addRect({100.5001f, 20, 200, 100});
158 canvas->drawPath(path, p);
159
160 canvas->translate(300, 0);
161 path.reset();
162 path.addRect({20, 20, 100.1f, 100});
163 path.addRect({100.9f, 20, 200, 100});
164 canvas->drawPath(path, p);
Yuqian Li131c1fb2016-12-12 16:24:47 -0500165 }
166
167private:
168 typedef skiagm::GM INHERITED;
169};
170
171class AnalyticAntiAliasInverseGM : public skiagm::GM {
172public:
173 AnalyticAntiAliasInverseGM() {}
174
175protected:
176
177 SkString onShortName() override {
178 return SkString("analytic_antialias_inverse");
179 }
180
181 SkISize onISize() override {
182 return SkISize::Make(W, H);
183 }
184
185 void onDraw(SkCanvas* canvas) override {
186 SkPaint p;
187 p.setColor(SK_ColorRED);
188 p.setAntiAlias(true);
189
190 canvas->save();
191
192 SkPath path;
193 path.addCircle(100, 100, 30);
194 path.setFillType(SkPath::kInverseWinding_FillType);
195 canvas->drawPath(path, p);
196 canvas->restore();
197 }
198
199private:
200 typedef skiagm::GM INHERITED;
201};
202
liyuqiane60d8552016-10-03 13:49:37 -0700203DEF_GM( return new AnalyticAntiAliasConvexGM; )
Yuqian Li131c1fb2016-12-12 16:24:47 -0500204DEF_GM( return new AnalyticAntiAliasGeneralGM; )
205DEF_GM( return new AnalyticAntiAliasInverseGM; )