• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "SkCanvas.h"
10 #include "SkTArray.h"
11 
12 namespace skiagm {
13 
14 class HairlinesGM : public GM {
15 protected:
16 
onShortName()17     virtual SkString onShortName() SK_OVERRIDE {
18         return SkString("hairlines");
19     }
20 
onISize()21     virtual SkISize onISize() { return make_isize(800, 600); }
22 
onOnceBeforeDraw()23     virtual void onOnceBeforeDraw() SK_OVERRIDE {
24         {
25             SkPath* lineAnglesPath = &fPaths.push_back();
26             enum {
27                 kNumAngles = 15,
28                 kRadius = 40,
29             };
30             for (int i = 0; i < kNumAngles; ++i) {
31                 SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles;
32                 SkScalar x = kRadius * SkScalarCos(angle);
33                 SkScalar y = kRadius * SkScalarSin(angle);
34                 lineAnglesPath->moveTo(x, y);
35                 lineAnglesPath->lineTo(-x, -y);
36             }
37         }
38 
39         {
40             SkPath* kindaTightQuad = &fPaths.push_back();
41             kindaTightQuad->moveTo(0, -10 * SK_Scalar1);
42             kindaTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -10 * SK_Scalar1, 0);
43         }
44 
45         {
46             SkPath* tightQuad = &fPaths.push_back();
47             tightQuad->moveTo(0, -5 * SK_Scalar1);
48             tightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -5 * SK_Scalar1, 0);
49         }
50 
51         {
52             SkPath* tighterQuad = &fPaths.push_back();
53             tighterQuad->moveTo(0, -2 * SK_Scalar1);
54             tighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -2 * SK_Scalar1, 0);
55         }
56 
57         {
58             SkPath* unevenTighterQuad = &fPaths.push_back();
59             unevenTighterQuad->moveTo(0, -1 * SK_Scalar1);
60             SkPoint p;
61             p.set(-2 * SK_Scalar1 + 3 * SkIntToScalar(102) / 4, SkIntToScalar(75));
62             unevenTighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), p.fX, p.fY);
63         }
64 
65         {
66             SkPath* reallyTightQuad = &fPaths.push_back();
67             reallyTightQuad->moveTo(0, -1 * SK_Scalar1);
68             reallyTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -1 * SK_Scalar1, 0);
69         }
70 
71         {
72             SkPath* closedQuad = &fPaths.push_back();
73             closedQuad->moveTo(0, -0);
74             closedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), 0, 0);
75         }
76 
77         {
78             SkPath* unevenClosedQuad = &fPaths.push_back();
79             unevenClosedQuad->moveTo(0, -0);
80             unevenClosedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100),
81                                      SkIntToScalar(75), SkIntToScalar(75));
82         }
83 
84         // Two problem cases for gpu hairline renderer found by shapeops testing. These used
85         // to assert that the computed bounding box didn't contain all the vertices.
86         {
87             SkPath* problem1 = &fPaths.push_back();
88             problem1->moveTo(SkIntToScalar(4), SkIntToScalar(6));
89             problem1->cubicTo(SkIntToScalar(5), SkIntToScalar(6),
90                               SkIntToScalar(5), SkIntToScalar(4),
91                               SkIntToScalar(4), SkIntToScalar(0));
92             problem1->close();
93         }
94 
95         {
96             SkPath* problem2 = &fPaths.push_back();
97             problem2->moveTo(SkIntToScalar(5), SkIntToScalar(1));
98             problem2->lineTo(4.32787323f, 1.67212653f);
99             problem2->cubicTo(2.75223875f, 3.24776125f,
100                               3.00581908f, 4.51236057f,
101                               3.7580452f, 4.37367964f);
102             problem2->cubicTo(4.66472578f, 3.888381f,
103                               5.f, 2.875f,
104                               5.f, 1.f);
105             problem2->close();
106         }
107 
108         // Three paths that show the same bug (missing end caps)
109         {
110             // A caret (crbug.com/131770)
111             SkPath* bug0 = &fPaths.push_back();
112             bug0->moveTo(6.5f,5.5f);
113             bug0->lineTo(3.5f,0.5f);
114             bug0->moveTo(0.5f,5.5f);
115             bug0->lineTo(3.5f,0.5f);
116         }
117 
118         {
119             // An X (crbug.com/137317)
120             SkPath* bug1 = &fPaths.push_back();
121 
122             bug1->moveTo(1, 1);
123             bug1->lineTo(6, 6);
124             bug1->moveTo(1, 6);
125             bug1->lineTo(6, 1);
126         }
127 
128         {
129             // A right angle (crbug.com/137465 and crbug.com/256776)
130             SkPath* bug2 = &fPaths.push_back();
131 
132             bug2->moveTo(5.5f, 5.5f);
133             bug2->lineTo(5.5f, 0.5f);
134             bug2->lineTo(0.5f, 0.5f);
135         }
136 
137         {
138             // Arc example to test imperfect truncation bug (crbug.com/295626)
139             static const SkScalar kRad = SkIntToScalar(2000);
140             static const SkScalar kStartAngle = 262.59717f;
141             static const SkScalar kSweepAngle = SkScalarHalf(17.188717f);
142 
143             SkPath* bug = &fPaths.push_back();
144 
145             // Add a circular arc
146             SkRect circle = SkRect::MakeLTRB(-kRad, -kRad, kRad, kRad);
147             bug->addArc(circle, kStartAngle, kSweepAngle);
148 
149             // Now add the chord that should cap the circular arc
150             SkScalar cosV, sinV = SkScalarSinCos(SkDegreesToRadians(kStartAngle), &cosV);
151 
152             SkPoint p0 = SkPoint::Make(kRad * cosV, kRad * sinV);
153 
154             sinV = SkScalarSinCos(SkDegreesToRadians(kStartAngle + kSweepAngle), &cosV);
155 
156             SkPoint p1 = SkPoint::Make(kRad * cosV, kRad * sinV);
157 
158             bug->moveTo(p0);
159             bug->lineTo(p1);
160         }
161     }
162 
onDraw(SkCanvas * canvas)163     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
164         static const SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
165 
166         enum {
167             kMargin = 5,
168         };
169         int wrapX = canvas->getDeviceSize().fWidth - kMargin;
170 
171         SkScalar maxH = 0;
172         canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin));
173         canvas->save();
174 
175         SkScalar x = SkIntToScalar(kMargin);
176         for (int p = 0; p < fPaths.count(); ++p) {
177             for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) {
178                 for (int aa = 0; aa < 2; ++aa) {
179                     const SkRect& bounds = fPaths[p].getBounds();
180 
181                     if (x + bounds.width() > wrapX) {
182                         canvas->restore();
183                         canvas->translate(0, maxH + SkIntToScalar(kMargin));
184                         canvas->save();
185                         maxH = 0;
186                         x = SkIntToScalar(kMargin);
187                     }
188 
189                     SkPaint paint;
190                     paint.setARGB(kAlphaValue[a], 0, 0, 0);
191                     paint.setAntiAlias(SkToBool(aa));
192                     paint.setStyle(SkPaint::kStroke_Style);
193                     paint.setStrokeWidth(0);
194 
195                     canvas->save();
196                     canvas->translate(-bounds.fLeft, -bounds.fTop);
197                     canvas->drawPath(fPaths[p], paint);
198                     canvas->restore();
199 
200                     maxH = SkMaxScalar(maxH, bounds.height());
201 
202                     SkScalar dx = bounds.width() + SkIntToScalar(kMargin);
203                     x += dx;
204                     canvas->translate(dx, 0);
205                 }
206             }
207         }
208         canvas->restore();
209     }
210 
211 private:
212     SkTArray<SkPath> fPaths;
213     typedef GM INHERITED;
214 };
215 
216 //////////////////////////////////////////////////////////////////////////////
217 
MyFactory(void *)218 static GM* MyFactory(void*) { return new HairlinesGM; }
219 static GMRegistry reg(MyFactory);
220 
221 }
222