Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: gm/convex_all_line_paths.cpp

Issue 1112603002: Add new GM/bench for line-only convex paths (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix another SK_ARRAY_COUNT issue Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 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
10 static void create_ngon(int n, SkPoint* pts, SkScalar width, SkScalar height) {
11 float angleStep = 360.0f / n, angle = 0.0f, sin, cos;
12 if ((n % 2) == 1) {
13 angle = angleStep/2.0f;
14 }
15
16 for (int i = 0; i < n; ++i) {
17 sin = SkScalarSinCos(SkDegreesToRadians(angle), &cos);
18 pts[i].fX = -sin * width;
19 pts[i].fY = cos * height;
20 angle += angleStep;
21 }
22 }
23
24 namespace skiagm {
25
26 // This GM is intended to exercise Ganesh's handling of convex line-only
27 // paths
28 class ConvexLineOnlyPathsGM : public GM {
29 public:
30 ConvexLineOnlyPathsGM() {
31 fOffset.set(0, SkScalarHalf(kMaxPathHeight));
32 this->setBGColor(0xFFFFFFFF);
33 }
34
35 protected:
36 SkString onShortName() override { return SkString("convex-lineonly-paths"); }
37 SkISize onISize() override { return SkISize::Make(kGMWidth, kGMHeight); }
38 bool runAsBench() const override { return true; }
39
40 static SkPath GetPath(int index, int offset, SkPath::Direction dir) {
41 // narrow rect
42 const SkPoint gPoints0[] = {
43 { -1.5f, -50.0f },
44 { 1.5f, -50.0f },
45 { 1.5f, 50.0f },
46 { -1.5f, 50.0f }
47 };
48 // narrow rect on an angle
49 const SkPoint gPoints1[] = {
50 { -50.0f, -49.0f },
51 { -49.0f, -50.0f },
52 { 50.0f, 49.0f },
53 { 49.0f, 50.0f }
54 };
55 // trap - narrow on top - wide on bottom
56 const SkPoint gPoints2[] = {
57 { -10.0f, -50.0f },
58 { 10.0f, -50.0f },
59 { 50.0f, 50.0f },
60 { -50.0f, 50.0f }
61 };
62 // wide skewed rect
63 const SkPoint gPoints3[] = {
64 { -50.0f, -50.0f },
65 { 0.0f, -50.0f },
66 { 50.0f, 50.0f },
67 { 0.0f, 50.0f }
68 };
69 // thin rect with colinear-ish lines
70 const SkPoint gPoints4[] = {
71 { -6.0f, -50.0f },
72 { 4.0f, -50.0f },
73 { 5.0f, -25.0f },
74 { 6.0f, 0.0f },
75 { 5.0f, 25.0f },
76 { 4.0f, 50.0f },
77 { -4.0f, 50.0f }
78 };
79 // degenerate
80 const SkPoint gPoints5[] = {
81 { -0.025f, -0.025f },
82 { 0.025f, -0.025f },
83 { 0.025f, 0.025f },
84 { -0.025f, 0.025f }
85 };
86 // Triangle in which the first point should fuse with last
87 const SkPoint gPoints6[] = {
88 { -20.0f, -13.0f },
89 { -20.0f, -13.05f },
90 { 20.0f, -13.0f },
91 { 20.0f, 27.0f }
92 };
93 // thin rect with colinear lines
94 const SkPoint gPoints7[] = {
95 { -10.0f, -50.0f },
96 { 10.0f, -50.0f },
97 { 10.0f, -25.0f },
98 { 10.0f, 0.0f },
99 { 10.0f, 25.0f },
100 { 10.0f, 50.0f },
101 { -10.0f, 50.0f }
102 };
103 // capped teardrop
104 const SkPoint gPoints8[] = {
105 { 50.00f, 50.00f },
106 { 0.00f, 50.00f },
107 { -15.45f, 47.55f },
108 { -29.39f, 40.45f },
109 { -40.45f, 29.39f },
110 { -47.55f, 15.45f },
111 { -50.00f, 0.00f },
112 { -47.55f, -15.45f },
113 { -40.45f, -29.39f },
114 { -29.39f, -40.45f },
115 { -15.45f, -47.55f },
116 { 0.00f, -50.00f },
117 { 50.00f, -50.00f }
118 };
119 // teardrop
120 const SkPoint gPoints9[] = {
121 { 4.39f, 40.45f },
122 { -9.55f, 47.55f },
123 { -25.00f, 50.00f },
124 { -40.45f, 47.55f },
125 { -54.39f, 40.45f },
126 { -65.45f, 29.39f },
127 { -72.55f, 15.45f },
128 { -75.00f, 0.00f },
129 { -72.55f, -15.45f },
130 { -65.45f, -29.39f },
131 { -54.39f, -40.45f },
132 { -40.45f, -47.55f },
133 { -25.0f, -50.0f },
134 { -9.55f, -47.55f },
135 { 4.39f, -40.45f },
136 { 75.00f, 0.00f }
137 };
138 // clipped triangle
139 const SkPoint gPoints10[] = {
140 { -10.0f, -50.0f },
141 { 10.0f, -50.0f },
142 { 50.0f, 31.0f },
143 { 40.0f, 50.0f },
144 { -40.0f, 50.0f },
145 { -50.0f, 31.0f },
146 };
147
148 const SkPoint* gPoints[] = {
149 gPoints0, gPoints1, gPoints2, gPoints3, gPoints4, gPoints5, gPoints6 ,
150 gPoints7, gPoints8, gPoints9, gPoints10,
151 };
152
153 const size_t gSizes[] = {
154 SK_ARRAY_COUNT(gPoints0),
155 SK_ARRAY_COUNT(gPoints1),
156 SK_ARRAY_COUNT(gPoints2),
157 SK_ARRAY_COUNT(gPoints3),
158 SK_ARRAY_COUNT(gPoints4),
159 SK_ARRAY_COUNT(gPoints5),
160 SK_ARRAY_COUNT(gPoints6),
161 SK_ARRAY_COUNT(gPoints7),
162 SK_ARRAY_COUNT(gPoints8),
163 SK_ARRAY_COUNT(gPoints9),
164 SK_ARRAY_COUNT(gPoints10),
165 };
166 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSizes) == SK_ARRAY_COUNT(gPoints), arr ay_mismatch);
167
168 SkAutoTDelete<SkPoint> data;
169 const SkPoint* points;
170 int numPts;
171 if (index < (int) SK_ARRAY_COUNT(gPoints)) {
172 // manually specified
173 points = gPoints[index];
174 numPts = (int) gSizes[index];
175 } else {
176 // procedurally generated
177 SkScalar width = kMaxPathHeight/2;
178 SkScalar height = kMaxPathHeight/2;
179 switch (index-SK_ARRAY_COUNT(gPoints)) {
180 case 0:
181 numPts = 3;
182 break;
183 case 1:
184 numPts = 4;
185 break;
186 case 2:
187 numPts = 5;
188 break;
189 case 3: // squashed pentagon
190 numPts = 5;
191 width = kMaxPathHeight/5;
192 break;
193 case 4:
194 numPts = 6;
195 break;
196 case 5:
197 numPts = 8;
198 break;
199 case 6: // squashed octogon
200 numPts = 8;
201 width = kMaxPathHeight/5;
202 break;
203 case 7:
204 numPts = 20;
205 break;
206 case 8:
207 numPts = 100;
208 break;
209 default:
210 numPts = 3;
211 break;
212 }
213
214 data.reset(SkNEW_ARRAY(SkPoint, numPts));
215
216 create_ngon(numPts, data.get(), width, height);
217 points = data.get();
218 }
219
220 SkPath path;
221
222 if (SkPath::kCW_Direction == dir) {
223 path.moveTo(points[0]);
224 for (int i = 1; i < numPts; ++i) {
225 path.lineTo(points[i]);
226 }
227 } else {
228 path.moveTo(points[numPts-1]);
229 for (int i = numPts-2; i >= 0; --i) {
230 path.lineTo(points[i]);
231 }
232 }
233
234 path.close();
235 #ifdef SK_DEBUG
236 // Each path this method returns should be convex, only composed of
237 // lines, wound the right direction, and short enough to fit in one
238 // of the GMs rows.
239 SkASSERT(path.isConvex());
240 SkASSERT(SkPath::kLine_SegmentMask == path.getSegmentMasks());
241 SkPath::Direction actualDir;
242 SkASSERT(path.cheapComputeDirection(&actualDir));
243 SkASSERT(dir == actualDir);
244 SkRect bounds = path.getBounds();
245 SkASSERT(SkScalarNearlyEqual(bounds.centerX(), 0.0f));
246 SkASSERT(bounds.height() <= kMaxPathHeight);
247 #endif
248 return path;
249 }
250
251 // Draw a single path several times, shrinking it, flipping its direction
252 // and changing its start vertex each time.
253 void drawPath(SkCanvas* canvas, int index) {
254
255 SkPoint center;
256 {
257 SkPath path = GetPath(index, 0, SkPath::kCW_Direction);
258 if (fOffset.fX+path.getBounds().width() > kGMWidth) {
259 fOffset.fX = 0;
260 fOffset.fY += kMaxPathHeight;
261 }
262 center = { fOffset.fX + SkScalarHalf(path.getBounds().width()), fOff set.fY};
263 fOffset.fX += path.getBounds().width();
264 }
265
266 const SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
267 const SkPath::Direction dirs[2] = { SkPath::kCW_Direction, SkPath::kCCW_ Direction };
268 const float scales[] = { 1.0f, 0.75f, 0.5f, 0.25f, 0.1f, 0.01f, 0.001f } ;
269
270 SkPaint paint;
271 paint.setAntiAlias(true);
272
273 for (size_t i = 0; i < SK_ARRAY_COUNT(scales); ++i) {
274 SkPath path = GetPath(index, (int) i, dirs[i%2]);
275
276 canvas->save();
277 canvas->translate(center.fX, center.fY);
278 canvas->scale(scales[i], scales[i]);
279 paint.setColor(colors[i%2]);
280 canvas->drawPath(path, paint);
281 canvas->restore();
282 }
283 }
284
285 void onDraw(SkCanvas* canvas) override {
286 for (int i = 0; i < kNumPaths; ++i) {
287 this->drawPath(canvas, i);
288 }
289
290 // Repro for crbug.com/472723 (Missing AA on portions of graphic with GP U rasterization)
291 {
292 canvas->translate(356.0f, 50.0f);
293
294 SkPaint p;
295 p.setAntiAlias(true);
296
297 SkPath p1;
298 p1.moveTo(60.8522949f, 364.671021f);
299 p1.lineTo(59.4380493f, 364.671021f);
300 p1.lineTo(385.414276f, 690.647217f);
301 p1.lineTo(386.121399f, 689.940125f);
302 canvas->drawPath(p1, p);
303 }
304 }
305
306 private:
307 static const int kNumPaths = 20;
308 static const int kMaxPathHeight = 100;
309 static const int kGMWidth = 512;
310 static const int kGMHeight = 512;
311
312 SkPoint fOffset; // the right edge of the last drawn path
313
314 typedef GM INHERITED;
315 };
316
317 //////////////////////////////////////////////////////////////////////////////
318
319 DEF_GM( return SkNEW(ConvexLineOnlyPathsGM); )
320
321 }
OLDNEW
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698