• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "gm.h"
9 #include "SkPath.h"
10 #include "SkRandom.h"
11 #include "SkTArray.h"
12 
13 class SkDoOnce : SkNoncopyable {
14 public:
SkDoOnce()15     SkDoOnce() { fDidOnce = false; }
16 
needToDo() const17     bool needToDo() const { return !fDidOnce; }
alreadyDone() const18     bool alreadyDone() const { return fDidOnce; }
accomplished()19     void accomplished() {
20         SkASSERT(!fDidOnce);
21         fDidOnce = true;
22     }
23 
24 private:
25     bool fDidOnce;
26 };
27 
28 namespace skiagm {
29 
30 class ConvexPathsGM : public GM {
31     SkDoOnce fOnce;
32 public:
ConvexPathsGM()33     ConvexPathsGM() {
34         this->setBGColor(0xFF000000);
35     }
36 
37 protected:
38 
onShortName()39     virtual SkString onShortName() {
40         return SkString("convexpaths");
41     }
42 
43 
onISize()44     virtual SkISize onISize() {
45         return SkISize::Make(1200, 1100);
46     }
47 
makePaths()48     void makePaths() {
49         if (fOnce.alreadyDone()) {
50             return;
51         }
52         fOnce.accomplished();
53 
54         fPaths.push_back().moveTo(0, 0);
55         fPaths.back().quadTo(50 * SK_Scalar1, 100 * SK_Scalar1,
56                              0, 100 * SK_Scalar1);
57         fPaths.back().lineTo(0, 0);
58 
59         fPaths.push_back().moveTo(0, 50 * SK_Scalar1);
60         fPaths.back().quadTo(50 * SK_Scalar1, 0,
61                              100 * SK_Scalar1, 50 * SK_Scalar1);
62         fPaths.back().quadTo(50 * SK_Scalar1, 100 * SK_Scalar1,
63                              0, 50 * SK_Scalar1);
64 
65         fPaths.push_back().addRect(0, 0,
66                                    100 * SK_Scalar1, 100 * SK_Scalar1,
67                                    SkPath::kCW_Direction);
68 
69         fPaths.push_back().addRect(0, 0,
70                                    100 * SK_Scalar1, 100 * SK_Scalar1,
71                                    SkPath::kCCW_Direction);
72 
73         fPaths.push_back().addCircle(50  * SK_Scalar1, 50  * SK_Scalar1,
74                                      50  * SK_Scalar1, SkPath::kCW_Direction);
75 
76 
77         fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0,
78                                                     50 * SK_Scalar1,
79                                                     100 * SK_Scalar1),
80                                    SkPath::kCW_Direction);
81 
82         fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0,
83                                                     100 * SK_Scalar1,
84                                                     5 * SK_Scalar1),
85                                    SkPath::kCCW_Direction);
86 
87         fPaths.push_back().addOval(SkRect::MakeXYWH(0, 0,
88                                                     SK_Scalar1,
89                                                     100 * SK_Scalar1),
90                                                     SkPath::kCCW_Direction);
91 
92         fPaths.push_back().addRoundRect(SkRect::MakeXYWH(0, 0,
93                                                          SK_Scalar1 * 100,
94                                                          SK_Scalar1 * 100),
95                                         40 * SK_Scalar1, 20 * SK_Scalar1,
96                                         SkPath::kCW_Direction);
97 
98         // large number of points
99         enum {
100             kLength = 100,
101             kPtsPerSide = (1 << 12),
102         };
103         fPaths.push_back().moveTo(0, 0);
104         for (int i = 1; i < kPtsPerSide; ++i) { // skip the first point due to moveTo.
105             fPaths.back().lineTo(kLength * SkIntToScalar(i) / kPtsPerSide, 0);
106         }
107         for (int i = 0; i < kPtsPerSide; ++i) {
108             fPaths.back().lineTo(kLength, kLength * SkIntToScalar(i) / kPtsPerSide);
109         }
110         for (int i = kPtsPerSide; i > 0; --i) {
111             fPaths.back().lineTo(kLength * SkIntToScalar(i) / kPtsPerSide, kLength);
112         }
113         for (int i = kPtsPerSide; i > 0; --i) {
114             fPaths.back().lineTo(0, kLength * SkIntToScalar(i) / kPtsPerSide);
115         }
116 
117         // shallow diagonals
118         fPaths.push_back().lineTo(100 * SK_Scalar1, SK_Scalar1);
119         fPaths.back().lineTo(98 * SK_Scalar1, 100 * SK_Scalar1);
120         fPaths.back().lineTo(3 * SK_Scalar1, 96 * SK_Scalar1);
121 
122         fPaths.push_back().arcTo(SkRect::MakeXYWH(0, 0,
123                                                   50 * SK_Scalar1,
124                                                   100 * SK_Scalar1),
125                                                   25 * SK_Scalar1,  130 * SK_Scalar1, false);
126 
127         // cubics
128         fPaths.push_back().cubicTo( 1 * SK_Scalar1,  1 * SK_Scalar1,
129                                    10 * SK_Scalar1,  90 * SK_Scalar1,
130                                     0 * SK_Scalar1, 100 * SK_Scalar1);
131         fPaths.push_back().cubicTo(100 * SK_Scalar1,  50 * SK_Scalar1,
132                                     20 * SK_Scalar1, 100 * SK_Scalar1,
133                                      0 * SK_Scalar1,   0 * SK_Scalar1);
134 
135         // path that has a cubic with a repeated first control point and
136         // a repeated last control point.
137         fPaths.push_back().moveTo(SK_Scalar1 * 10, SK_Scalar1 * 10);
138         fPaths.back().cubicTo(10 * SK_Scalar1, 10 * SK_Scalar1,
139                               10 * SK_Scalar1, 0,
140                               20 * SK_Scalar1, 0);
141         fPaths.back().lineTo(40 * SK_Scalar1, 0);
142         fPaths.back().cubicTo(40 * SK_Scalar1, 0,
143                               50 * SK_Scalar1, 0,
144                               50 * SK_Scalar1, 10 * SK_Scalar1);
145 
146         // path that has two cubics with repeated middle control points.
147         fPaths.push_back().moveTo(SK_Scalar1 * 10, SK_Scalar1 * 10);
148         fPaths.back().cubicTo(10 * SK_Scalar1, 0,
149                               10 * SK_Scalar1, 0,
150                               20 * SK_Scalar1, 0);
151         fPaths.back().lineTo(40 * SK_Scalar1, 0);
152         fPaths.back().cubicTo(50 * SK_Scalar1, 0,
153                               50 * SK_Scalar1, 0,
154                               50 * SK_Scalar1, 10 * SK_Scalar1);
155 
156         // cubic where last three points are almost a line
157         fPaths.push_back().moveTo(0, 228 * SK_Scalar1 / 8);
158         fPaths.back().cubicTo(628 * SK_Scalar1 / 8, 82 * SK_Scalar1 / 8,
159                               1255 * SK_Scalar1 / 8, 141 * SK_Scalar1 / 8,
160                               1883 * SK_Scalar1 / 8, 202 * SK_Scalar1 / 8);
161 
162         // flat cubic where the at end point tangents both point outward.
163         fPaths.push_back().moveTo(10 * SK_Scalar1, 0);
164         fPaths.back().cubicTo(0, SK_Scalar1,
165                               30 * SK_Scalar1, SK_Scalar1,
166                               20 * SK_Scalar1, 0);
167 
168         // flat cubic where initial tangent is in, end tangent out
169         fPaths.push_back().moveTo(0, 0 * SK_Scalar1);
170         fPaths.back().cubicTo(10 * SK_Scalar1, SK_Scalar1,
171                               30 * SK_Scalar1, SK_Scalar1,
172                               20 * SK_Scalar1, 0);
173 
174         // flat cubic where initial tangent is out, end tangent in
175         fPaths.push_back().moveTo(10 * SK_Scalar1, 0);
176         fPaths.back().cubicTo(0, SK_Scalar1,
177                               20 * SK_Scalar1, SK_Scalar1,
178                               30 * SK_Scalar1, 0);
179 
180         // triangle where one edge is a degenerate quad
181         fPaths.push_back().moveTo(8.59375f, 45 * SK_Scalar1);
182         fPaths.back().quadTo(16.9921875f,   45 * SK_Scalar1,
183                              31.25f,        45 * SK_Scalar1);
184         fPaths.back().lineTo(100 * SK_Scalar1,              100 * SK_Scalar1);
185         fPaths.back().lineTo(8.59375f,      45 * SK_Scalar1);
186 
187         // triangle where one edge is a quad with a repeated point
188         fPaths.push_back().moveTo(0, 25 * SK_Scalar1);
189         fPaths.back().lineTo(50 * SK_Scalar1, 0);
190         fPaths.back().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1, 50 * SK_Scalar1, 50 * SK_Scalar1);
191 
192         // triangle where one edge is a cubic with a 2x repeated point
193         fPaths.push_back().moveTo(0, 25 * SK_Scalar1);
194         fPaths.back().lineTo(50 * SK_Scalar1, 0);
195         fPaths.back().cubicTo(50 * SK_Scalar1, 0,
196                               50 * SK_Scalar1, 50 * SK_Scalar1,
197                               50 * SK_Scalar1, 50 * SK_Scalar1);
198 
199         // triangle where one edge is a quad with a nearly repeated point
200         fPaths.push_back().moveTo(0, 25 * SK_Scalar1);
201         fPaths.back().lineTo(50 * SK_Scalar1, 0);
202         fPaths.back().quadTo(50 * SK_Scalar1, 49.95f,
203                              50 * SK_Scalar1, 50 * SK_Scalar1);
204 
205         // triangle where one edge is a cubic with a 3x nearly repeated point
206         fPaths.push_back().moveTo(0, 25 * SK_Scalar1);
207         fPaths.back().lineTo(50 * SK_Scalar1, 0);
208         fPaths.back().cubicTo(50 * SK_Scalar1, 49.95f,
209                               50 * SK_Scalar1, 49.97f,
210                               50 * SK_Scalar1, 50 * SK_Scalar1);
211 
212         // triangle where there is a point degenerate cubic at one corner
213         fPaths.push_back().moveTo(0, 25 * SK_Scalar1);
214         fPaths.back().lineTo(50 * SK_Scalar1, 0);
215         fPaths.back().lineTo(50 * SK_Scalar1, 50 * SK_Scalar1);
216         fPaths.back().cubicTo(50 * SK_Scalar1, 50 * SK_Scalar1,
217                               50 * SK_Scalar1, 50 * SK_Scalar1,
218                               50 * SK_Scalar1, 50 * SK_Scalar1);
219 
220         // point line
221         fPaths.push_back().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1);
222         fPaths.back().lineTo(50 * SK_Scalar1, 50 * SK_Scalar1);
223 
224         // point quad
225         fPaths.push_back().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1);
226         fPaths.back().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1,
227                              50 * SK_Scalar1, 50 * SK_Scalar1);
228 
229         // point cubic
230         fPaths.push_back().moveTo(50 * SK_Scalar1, 50 * SK_Scalar1);
231         fPaths.back().cubicTo(50 * SK_Scalar1, 50 * SK_Scalar1,
232                               50 * SK_Scalar1, 50 * SK_Scalar1,
233                               50 * SK_Scalar1, 50 * SK_Scalar1);
234 
235         // moveTo only paths
236         fPaths.push_back().moveTo(0, 0);
237         fPaths.back().moveTo(0, 0);
238         fPaths.back().moveTo(SK_Scalar1, SK_Scalar1);
239         fPaths.back().moveTo(SK_Scalar1, SK_Scalar1);
240         fPaths.back().moveTo(10 * SK_Scalar1, 10 * SK_Scalar1);
241 
242         fPaths.push_back().moveTo(0, 0);
243         fPaths.back().moveTo(0, 0);
244 
245         // line degenerate
246         fPaths.push_back().lineTo(100 * SK_Scalar1, 100 * SK_Scalar1);
247         fPaths.push_back().quadTo(100 * SK_Scalar1, 100 * SK_Scalar1, 0, 0);
248         fPaths.push_back().quadTo(100 * SK_Scalar1, 100 * SK_Scalar1,
249                                   50 * SK_Scalar1, 50 * SK_Scalar1);
250         fPaths.push_back().quadTo(50 * SK_Scalar1, 50 * SK_Scalar1,
251                                   100 * SK_Scalar1, 100 * SK_Scalar1);
252         fPaths.push_back().cubicTo(0, 0,
253                                    0, 0,
254                                    100 * SK_Scalar1, 100 * SK_Scalar1);
255 
256         // small circle. This is listed last so that it has device coords far
257         // from the origin (small area relative to x,y values).
258         fPaths.push_back().addCircle(0, 0, 1.2f);
259     }
260 
onDraw(SkCanvas * canvas)261     virtual void onDraw(SkCanvas* canvas) {
262         this->makePaths();
263 
264     SkPaint paint;
265     paint.setAntiAlias(true);
266     SkRandom rand;
267     canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1);
268 
269     // As we've added more paths this has gotten pretty big. Scale the whole thing down.
270     canvas->scale(2 * SK_Scalar1 / 3, 2 * SK_Scalar1 / 3);
271 
272     for (int i = 0; i < fPaths.count(); ++i) {
273         canvas->save();
274         // position the path, and make it at off-integer coords.
275         canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 10,
276                           SK_Scalar1 * 200 * (i / 5) + 9 * SK_Scalar1 / 10);
277         SkColor color = rand.nextU();
278         color |= 0xff000000;
279         paint.setColor(color);
280 #if 0 // This hitting on 32bit Linux builds for some paths. Temporarily disabling while it is
281       // debugged.
282         SkASSERT(fPaths[i].isConvex());
283 #endif
284         canvas->drawPath(fPaths[i], paint);
285         canvas->restore();
286     }
287     }
288 
289 private:
290     typedef GM INHERITED;
291     SkTArray<SkPath> fPaths;
292 };
293 
294 //////////////////////////////////////////////////////////////////////////////
295 
MyFactory(void *)296 static GM* MyFactory(void*) { return new ConvexPathsGM; }
297 static GMRegistry reg(MyFactory);
298 
299 }
300