xref: /aosp_15_r20/external/skia/gm/hairlines.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2013 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/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/private/base/SkTArray.h"
20 
21 using namespace skia_private;
22 
23 namespace skiagm {
24 
25 class HairlinesGM : public GM {
26 protected:
getName() const27     SkString getName() const override { return SkString("hairlines"); }
28 
getISize()29     SkISize getISize() override { return SkISize::Make(1250, 1250); }
30 
onOnceBeforeDraw()31     void onOnceBeforeDraw() override {
32         {
33             SkPathBuilder lineAngles;
34             constexpr int kNumAngles = 15;
35             constexpr int kRadius = 40;
36 
37             for (int i = 0; i < kNumAngles; ++i) {
38                 SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles;
39                 SkScalar x = kRadius * SkScalarCos(angle);
40                 SkScalar y = kRadius * SkScalarSin(angle);
41                 lineAngles.moveTo(x, y).lineTo(-x, -y);
42             }
43             fPaths.push_back(lineAngles.detach());
44         }
45 
46         fPaths.push_back(SkPathBuilder().moveTo(0, -10)
47                                         .quadTo(100, 100, -10, 0)
48                                         .detach());
49 
50         fPaths.push_back(SkPathBuilder().moveTo(0, -5)
51                                         .quadTo(100, 100, -5, 0)
52                                         .detach());
53 
54         fPaths.push_back(SkPathBuilder().moveTo(0, -2)
55                                         .quadTo(100, 100, -2, 0)
56                                         .detach());
57 
58         fPaths.push_back(SkPathBuilder().moveTo(0, -1)
59                                         .quadTo(100, 100, -2 + 306.0f / 4, 75)
60                                         .detach());
61 
62         fPaths.push_back(SkPathBuilder().moveTo(0, -1)
63                                         .quadTo(100, 100, -1, 0)
64                                         .detach());
65 
66         fPaths.push_back(SkPathBuilder().moveTo(0, -0)
67                                         .quadTo(100, 100, 0, 0)
68                                         .detach());
69 
70         fPaths.push_back(SkPathBuilder().moveTo(0, -0)
71                                         .quadTo(100, 100, 75, 75)
72                                         .detach());
73 
74         // Two problem cases for gpu hairline renderer found by shapeops testing. These used
75         // to assert that the computed bounding box didn't contain all the vertices.
76 
77         fPaths.push_back(SkPathBuilder().moveTo(4, 6)
78                                         .cubicTo(5, 6, 5, 4, 4, 0)
79                                         .close()
80                                         .detach());
81 
82         fPaths.push_back(SkPathBuilder().moveTo(5, 1)
83                                         .lineTo( 4.32787323f, 1.67212653f)
84                                         .cubicTo(2.75223875f, 3.24776125f,
85                                                  3.00581908f, 4.51236057f,
86                                                  3.7580452f,  4.37367964f)
87                                         .cubicTo(4.66472578f, 3.888381f,
88                                                  5.f,         2.875f,
89                                                  5.f,         1.f)
90                                         .close()
91                                         .detach());
92 
93         // Three paths that show the same bug (missing end caps)
94 
95         fPaths.push_back(SkPathBuilder().moveTo(6.5f,5.5f)
96                                         .lineTo(3.5f,0.5f)
97                                         .moveTo(0.5f,5.5f)
98                                         .lineTo(3.5f,0.5f)
99                                         .detach());
100 
101         // An X (crbug.com/137317)
102         fPaths.push_back(SkPathBuilder().moveTo(1, 1)
103                                         .lineTo(6, 6)
104                                         .moveTo(1, 6)
105                                         .lineTo(6, 1)
106                                         .detach());
107 
108         // A right angle (crbug.com/137465 and crbug.com/256776)
109         fPaths.push_back(SkPathBuilder().moveTo(5.5f, 5.5f)
110                                         .lineTo(5.5f, 0.5f)
111                                         .lineTo(0.5f, 0.5f)
112                                         .detach());
113 
114         {
115             // Arc example to test imperfect truncation bug (crbug.com/295626)
116             constexpr SkScalar kRad = SkIntToScalar(2000);
117             constexpr SkScalar kStartAngle = 262.59717f;
118             constexpr SkScalar kSweepAngle = SkScalarHalf(17.188717f);
119 
120             SkPathBuilder bug;
121 
122             // Add a circular arc
123             SkRect circle = SkRect::MakeLTRB(-kRad, -kRad, kRad, kRad);
124             bug.addArc(circle, kStartAngle, kSweepAngle);
125 
126             // Now add the chord that should cap the circular arc
127             SkPoint p0 = { kRad * SkScalarCos(SkDegreesToRadians(kStartAngle)),
128                            kRad * SkScalarSin(SkDegreesToRadians(kStartAngle)) };
129 
130             SkPoint p1 = { kRad * SkScalarCos(SkDegreesToRadians(kStartAngle + kSweepAngle)),
131                            kRad * SkScalarSin(SkDegreesToRadians(kStartAngle + kSweepAngle)) };
132 
133             bug.moveTo(p0);
134             bug.lineTo(p1);
135             fPaths.push_back(bug.detach());
136         }
137     }
138 
onDraw(SkCanvas * canvas)139     void onDraw(SkCanvas* canvas) override {
140         constexpr SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
141         constexpr SkScalar kWidths[] = { 0, 0.5f, 1.5f };
142 
143         enum {
144             kMargin = 5,
145         };
146         int wrapX = 1250 - kMargin;
147 
148         SkScalar maxH = 0;
149         canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin));
150         canvas->save();
151 
152         SkScalar x = SkIntToScalar(kMargin);
153         for (int p = 0; p < fPaths.size(); ++p) {
154             for (size_t a = 0; a < std::size(kAlphaValue); ++a) {
155                 for (int aa = 0; aa < 2; ++aa) {
156                     for (size_t w = 0; w < std::size(kWidths); w++) {
157                         const SkRect& bounds = fPaths[p].getBounds();
158 
159                         if (x + bounds.width() > wrapX) {
160                             canvas->restore();
161                             canvas->translate(0, maxH + SkIntToScalar(kMargin));
162                             canvas->save();
163                             maxH = 0;
164                             x = SkIntToScalar(kMargin);
165                         }
166 
167                         SkPaint paint;
168                         paint.setARGB(kAlphaValue[a], 0, 0, 0);
169                         paint.setAntiAlias(SkToBool(aa));
170                         paint.setStyle(SkPaint::kStroke_Style);
171                         paint.setStrokeWidth(kWidths[w]);
172 
173                         canvas->save();
174                         canvas->translate(-bounds.fLeft, -bounds.fTop);
175                         canvas->drawPath(fPaths[p], paint);
176                         canvas->restore();
177 
178                         maxH = std::max(maxH, bounds.height());
179 
180                         SkScalar dx = bounds.width() + SkIntToScalar(kMargin);
181                         x += dx;
182                         canvas->translate(dx, 0);
183                     }
184                 }
185             }
186         }
187         canvas->restore();
188     }
189 
190 private:
191     TArray<SkPath> fPaths;
192     using INHERITED = GM;
193 };
194 
draw_squarehair_tests(SkCanvas * canvas,SkScalar width,SkPaint::Cap cap,bool aa)195 static void draw_squarehair_tests(SkCanvas* canvas, SkScalar width, SkPaint::Cap cap, bool aa) {
196     SkPaint paint;
197     paint.setStrokeCap(cap);
198     paint.setStrokeWidth(width);
199     paint.setAntiAlias(aa);
200     paint.setStyle(SkPaint::kStroke_Style);
201     canvas->drawLine(10, 10, 20, 10, paint);
202     canvas->drawLine(30, 10, 30, 20, paint);
203     canvas->drawLine(40, 10, 50, 20, paint);
204     SkPathBuilder path;
205     path.moveTo(60, 10);
206     path.quadTo(60, 20, 70, 20);
207     path.conicTo(70, 10, 80, 10, 0.707f);
208     canvas->drawPath(path.detach(), paint);
209 
210     path.moveTo(90, 10);
211     path.cubicTo(90, 20, 100, 20, 100, 10);
212     path.lineTo(110, 10);
213     canvas->drawPath(path.detach(), paint);
214     canvas->translate(0, 30);
215 }
216 
217 DEF_SIMPLE_GM(squarehair, canvas, 240, 360) {
218     const bool aliases[] = { false, true };
219     const SkScalar widths[] = { 0, 0.999f, 1, 1.001f };
220     const SkPaint::Cap caps[] = { SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap };
221     for (auto alias : aliases) {
222         canvas->save();
223         for (auto width : widths) {
224             for (auto cap : caps) {
225                 draw_squarehair_tests(canvas, width, cap, alias);
226             }
227         }
228         canvas->restore();
229         canvas->translate(120, 0);
230     }
231 }
232 
233 // GM to test subdivision of hairlines
draw_subdivided_quad(SkCanvas * canvas,int x0,int y0,int x1,int y1,SkColor color)234 static void draw_subdivided_quad(SkCanvas* canvas, int x0, int y0, int x1, int y1, SkColor color) {
235     SkPaint paint;
236     paint.setStrokeWidth(1);
237     paint.setAntiAlias(true);
238     paint.setStyle(SkPaint::kStroke_Style);
239     paint.setColor(color);
240 
241     canvas->drawPath(SkPathBuilder().moveTo(0,0)
242                                     .quadTo(SkIntToScalar(x0), SkIntToScalar(y0),
243                                             SkIntToScalar(x1), SkIntToScalar(y1))
244                                     .detach(),
245                      paint);
246 }
247 
248 DEF_SIMPLE_GM(hairline_subdiv, canvas, 512, 256) {
249     // no subdivisions
250     canvas->translate(45, -25);
251     draw_subdivided_quad(canvas, 334, 334, 467, 267, SK_ColorBLACK);
252 
253     // one subdivision
254     canvas->translate(-185, -150);
255     draw_subdivided_quad(canvas, 472, 472, 660, 378, SK_ColorRED);
256 
257     // two subdivisions
258     canvas->translate(-275, -200);
259     draw_subdivided_quad(canvas, 668, 668, 934, 535, SK_ColorGREEN);
260 
261     // three subdivisions
262     canvas->translate(-385, -260);
263     draw_subdivided_quad(canvas, 944, 944, 1320, 756, SK_ColorBLUE);
264 }
265 
266 //////////////////////////////////////////////////////////////////////////////
267 
268 DEF_GM( return new HairlinesGM; )
269 
270 }  // namespace skiagm
271