blob: d056b0b53a756ed442cedf06c6a9c4e82251d6c5 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040013#include "include/core/SkScalar.h"
14#include "include/private/SkFloatBits.h"
liyuqiane60d8552016-10-03 13:49:37 -070015
16#define W 800
17#define H 800
18
Hal Canary607c44f2019-01-23 10:40:02 -050019DEF_SIMPLE_GM(analytic_antialias_convex, canvas, W, H) {
liyuqiane60d8552016-10-03 13:49:37 -070020 SkPaint p;
21 p.setColor(SK_ColorRED);
22 p.setAntiAlias(true);
23
24 canvas->clear(0xFFFFFFFF);
25
26 canvas->save();
27
28 SkScalar y = 0;
29
30 canvas->translate(0, y);
31 canvas->rotate(1);
Mike Reed3661bc92017-02-22 13:21:42 -050032 canvas->drawRect({ 20, 20, 200, 200 }, p);
liyuqiane60d8552016-10-03 13:49:37 -070033 canvas->restore();
34
35 y += 200;
36
Yuqian Li63f69cd2018-04-18 15:09:44 -040037 canvas->save();
liyuqiane60d8552016-10-03 13:49:37 -070038 canvas->translate(0, y);
39 canvas->rotate(1);
Mike Reed3661bc92017-02-22 13:21:42 -050040 canvas->drawRect({ 20, 20, 20.2f, 200 }, p);
41 canvas->drawRect({ 20, 200, 200, 200.1f }, p);
liyuqiane60d8552016-10-03 13:49:37 -070042 canvas->drawCircle(100, 100, 30, p);
43 canvas->restore();
liyuqian451ceba2016-11-07 08:10:44 -080044
45 // The following path is empty but it'll reveal bug chrome:662914
46 SkPath path;
47 path.moveTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f
48 // 77.8075f, 231.626f, 77.8074f, 231.625f, 77.8073f, 231.625f
49 path.cubicTo(SkBits2Float(0x429b9d71), SkBits2Float(0x4367a022),
50 SkBits2Float(0x429b9d64), SkBits2Float(0x4367a009),
51 SkBits2Float(0x429b9d50), SkBits2Float(0x43679ff2));
52 path.lineTo(SkBits2Float(0x429b9d5c), SkBits2Float(0x4367a041)); // 77.8073f, 231.626f
53 path.close();
54 canvas->drawPath(path, p);
Yuqian Li0038b7f2016-11-14 11:51:41 -050055
56 // The following path reveals a subtle SkAnalyticQuadraticEdge::updateQuadratic bug:
57 // we should not use any snapped y for the intermediate values whose error may accumulate;
58 // snapping should only be allowed once before updateLine.
59 path.reset();
60 path.moveTo(SkBits2Float(0x434ba71e), SkBits2Float(0x438a06d0)); // 203.653f, 276.053f
61 path.lineTo(SkBits2Float(0x43492a74), SkBits2Float(0x4396d70d)); // 201.166f, 301.68f
62 // 200.921f, 304.207f, 196.939f, 303.82f, 0.707107f
63 path.conicTo(SkBits2Float(0x4348ebaf), SkBits2Float(0x43981a75),
64 SkBits2Float(0x4344f079), SkBits2Float(0x4397e900), SkBits2Float(0x3f3504f3));
65 path.close();
66 // Manually setting convexity is required. Otherwise, this path will be considered concave.
67 path.setConvexity(SkPath::kConvex_Convexity);
68 canvas->drawPath(path, p);
Yuqian Li4a657ad2018-02-07 19:04:34 +080069
70 // skbug.com/7573
71 y += 200;
Yuqian Li63f69cd2018-04-18 15:09:44 -040072 canvas->save();
Yuqian Li4a657ad2018-02-07 19:04:34 +080073 canvas->translate(0, y);
74 p.setAntiAlias(true);
75 path.reset();
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, p);
81 canvas->restore();
Yuqian Li63f69cd2018-04-18 15:09:44 -040082
83 // skbug.com/7813
84 // t8888 splits the 800-high canvas into 3 pieces; the boundary is close to 266 and 534
85 path.reset();
86 path.moveTo(700, 266);
87 path.lineTo(710, 266);
88 path.lineTo(710, 534);
89 path.lineTo(700, 534);
90 canvas->drawPath(path, p);
Hal Canary607c44f2019-01-23 10:40:02 -050091}
liyuqiane60d8552016-10-03 13:49:37 -070092
Hal Canary607c44f2019-01-23 10:40:02 -050093DEF_SIMPLE_GM(analytic_antialias_general, canvas, W, H) {
Yuqian Li131c1fb2016-12-12 16:24:47 -050094 SkPaint p;
95 p.setColor(SK_ColorRED);
96 p.setAntiAlias(true);
97
98 canvas->clear(0xFFFFFFFF);
99
100 canvas->save();
101 canvas->rotate(1);
102 const SkScalar R = 115.2f, C = 128.0f;
103 SkPath path;
104 path.moveTo(C + R, C);
105 for (int i = 1; i < 8; ++i) {
106 SkScalar a = 2.6927937f * i;
Brian Osman4428f2c2019-04-02 10:59:28 -0400107 path.lineTo(C + R * SkScalarCos(a), C + R * SkScalarSin(a));
Yuqian Li131c1fb2016-12-12 16:24:47 -0500108 }
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();
Yuqian Lifbe07932017-11-07 11:19:52 -0500119
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 path.reset();
126 path.addRect({20, 20, 100.4999f, 100});
127 path.addRect({100.5001f, 20, 200, 100});
128 canvas->drawPath(path, p);
129
130 canvas->translate(300, 0);
131 path.reset();
132 path.addRect({20, 20, 100.1f, 100});
133 path.addRect({100.9f, 20, 200, 100});
134 canvas->drawPath(path, p);
Hal Canary607c44f2019-01-23 10:40:02 -0500135}
Yuqian Li131c1fb2016-12-12 16:24:47 -0500136
Hal Canary607c44f2019-01-23 10:40:02 -0500137DEF_SIMPLE_GM(analytic_antialias_inverse, canvas, W, H) {
Yuqian Li131c1fb2016-12-12 16:24:47 -0500138 SkPaint p;
139 p.setColor(SK_ColorRED);
140 p.setAntiAlias(true);
141
142 canvas->save();
143
144 SkPath path;
145 path.addCircle(100, 100, 30);
146 path.setFillType(SkPath::kInverseWinding_FillType);
147 canvas->drawPath(path, p);
148 canvas->restore();
Hal Canary607c44f2019-01-23 10:40:02 -0500149}