• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkPath.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/SkFloatBits.h"
15 
16 #define W   800
17 #define H   800
18 
DEF_SIMPLE_GM(analytic_antialias_convex,canvas,W,H)19 DEF_SIMPLE_GM(analytic_antialias_convex, canvas, W, H) {
20         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);
32         canvas->drawRect({ 20, 20, 200, 200 }, p);
33         canvas->restore();
34 
35         y += 200;
36 
37         canvas->save();
38         canvas->translate(0, y);
39         canvas->rotate(1);
40         canvas->drawRect({ 20, 20, 20.2f, 200 }, p);
41         canvas->drawRect({ 20, 200, 200, 200.1f }, p);
42         canvas->drawCircle(100, 100, 30, p);
43         canvas->restore();
44 
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);
55 
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.setConvexityType(SkPathConvexityType::kConvex);
68         canvas->drawPath(path, p);
69 
70         // skbug.com/7573
71         y += 200;
72         canvas->save();
73         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();
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.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);
91 }
92 
DEF_SIMPLE_GM(analytic_antialias_general,canvas,W,H)93 DEF_SIMPLE_GM(analytic_antialias_general, canvas, W, H) {
94         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;
107             path.lineTo(C + R * SkScalarCos(a), C + R * SkScalarSin(a));
108         }
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         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);
135 }
136 
DEF_SIMPLE_GM(analytic_antialias_inverse,canvas,W,H)137 DEF_SIMPLE_GM(analytic_antialias_inverse, canvas, W, H) {
138         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(SkPathFillType::kInverseWinding);
147         canvas->drawPath(path, p);
148         canvas->restore();
149 }
150