1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBenchmark.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkDashPathEffect.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkRandom.h"
#include "SkString.h"
#include "SkTDArray.h"
/*
* Cases to consider:
*
* 1. antialiasing on/off (esp. width <= 1)
* 2. strokewidth == 0, 1, 2
* 3. hline, vline, diagonal, rect, oval
* 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2])
*/
static void path_hline(SkPath* path) {
path->moveTo(SkIntToScalar(10), SkIntToScalar(10));
path->lineTo(SkIntToScalar(600), SkIntToScalar(10));
}
class DashBench : public SkBenchmark {
protected:
SkString fName;
SkTDArray<SkScalar> fIntervals;
int fWidth;
SkPoint fPts[2];
bool fDoClip;
public:
DashBench(const SkScalar intervals[], int count, int width,
bool doClip = false) {
fIntervals.append(count, intervals);
for (int i = 0; i < count; ++i) {
fIntervals[i] *= width;
}
fWidth = width;
fName.printf("dash_%d_%s", width, doClip ? "clipped" : "noclip");
fDoClip = doClip;
fPts[0].set(SkIntToScalar(10), SkIntToScalar(10));
fPts[1].set(SkIntToScalar(600), SkIntToScalar(10));
}
virtual void makePath(SkPath* path) {
path_hline(path);
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkPaint paint;
this->setupPaint(&paint);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SkIntToScalar(fWidth));
paint.setAntiAlias(false);
SkPath path;
this->makePath(&path);
paint.setPathEffect(new SkDashPathEffect(fIntervals.begin(),
fIntervals.count(), 0))->unref();
if (fDoClip) {
SkRect r = path.getBounds();
r.inset(-SkIntToScalar(20), -SkIntToScalar(20));
// now move it so we don't intersect
r.offset(0, r.height() * 3 / 2);
canvas->clipRect(r);
}
this->handlePath(canvas, path, paint, this->getLoops());
}
virtual void handlePath(SkCanvas* canvas, const SkPath& path,
const SkPaint& paint, int N) {
for (int i = 0; i < N; ++i) {
// canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint);
canvas->drawPath(path, paint);
}
}
private:
typedef SkBenchmark INHERITED;
};
class RectDashBench : public DashBench {
public:
RectDashBench(const SkScalar intervals[], int count, int width)
: INHERITED(intervals, count, width) {
fName.append("_rect");
}
protected:
virtual void handlePath(SkCanvas* canvas, const SkPath& path,
const SkPaint& paint, int N) SK_OVERRIDE {
SkPoint pts[2];
if (!path.isLine(pts) || pts[0].fY != pts[1].fY) {
this->INHERITED::handlePath(canvas, path, paint, N);
} else {
SkRect rect;
rect.fLeft = pts[0].fX;
rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2;
rect.fRight = rect.fLeft + SkIntToScalar(fWidth);
rect.fBottom = rect.fTop + paint.getStrokeWidth();
SkPaint p(paint);
p.setStyle(SkPaint::kFill_Style);
p.setPathEffect(NULL);
int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth));
SkScalar dx = SkIntToScalar(2 * fWidth);
for (int i = 0; i < N*10; ++i) {
SkRect r = rect;
for (int j = 0; j < count; ++j) {
canvas->drawRect(r, p);
r.offset(dx, 0);
}
}
}
}
private:
typedef DashBench INHERITED;
};
static void make_unit_star(SkPath* path, int n) {
SkScalar rad = -SK_ScalarPI / 2;
const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
path->moveTo(0, -SK_Scalar1);
for (int i = 1; i < n; i++) {
rad += drad;
SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
path->lineTo(cosV, sinV);
}
path->close();
}
static void make_poly(SkPath* path) {
make_unit_star(path, 9);
SkMatrix matrix;
matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
path->transform(matrix);
}
static void make_quad(SkPath* path) {
SkScalar x0 = SkIntToScalar(10);
SkScalar y0 = SkIntToScalar(10);
path->moveTo(x0, y0);
path->quadTo(x0, y0 + 400 * SK_Scalar1,
x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1);
}
static void make_cubic(SkPath* path) {
SkScalar x0 = SkIntToScalar(10);
SkScalar y0 = SkIntToScalar(10);
path->moveTo(x0, y0);
path->cubicTo(x0, y0 + 400 * SK_Scalar1,
x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1,
x0 + 600 * SK_Scalar1, y0);
}
class MakeDashBench : public SkBenchmark {
SkString fName;
SkPath fPath;
SkAutoTUnref<SkPathEffect> fPE;
public:
MakeDashBench(void (*proc)(SkPath*), const char name[]) {
fName.printf("makedash_%s", name);
proc(&fPath);
SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) };
fPE.reset(new SkDashPathEffect(vals, 2, 0));
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(SkCanvas*) SK_OVERRIDE {
SkPath dst;
for (int i = 0; i < this->getLoops(); ++i) {
SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
fPE->filterPath(&dst, fPath, &rec, NULL);
dst.rewind();
}
}
private:
typedef SkBenchmark INHERITED;
};
/*
* We try to special case square dashes (intervals are equal to strokewidth).
*/
class DashLineBench : public SkBenchmark {
SkString fName;
SkScalar fStrokeWidth;
bool fIsRound;
SkAutoTUnref<SkPathEffect> fPE;
public:
DashLineBench(SkScalar width, bool isRound) {
fName.printf("dashline_%g_%s", SkScalarToFloat(width), isRound ? "circle" : "square");
fStrokeWidth = width;
fIsRound = isRound;
SkScalar vals[] = { SK_Scalar1, SK_Scalar1 };
fPE.reset(new SkDashPathEffect(vals, 2, 0));
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkPaint paint;
this->setupPaint(&paint);
paint.setStrokeWidth(fStrokeWidth);
paint.setStrokeCap(fIsRound ? SkPaint::kRound_Cap : SkPaint::kSquare_Cap);
paint.setPathEffect(fPE);
for (int i = 0; i < this->getLoops(); ++i) {
canvas->drawLine(10 * SK_Scalar1, 10 * SK_Scalar1,
640 * SK_Scalar1, 10 * SK_Scalar1, paint);
}
}
private:
typedef SkBenchmark INHERITED;
};
class DrawPointsDashingBench : public SkBenchmark {
SkString fName;
int fStrokeWidth;
bool fDoAA;
SkAutoTUnref<SkPathEffect> fPathEffect;
public:
DrawPointsDashingBench(int dashLength, int strokeWidth, bool doAA)
{
fName.printf("drawpointsdash_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
fStrokeWidth = strokeWidth;
fDoAA = doAA;
SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
fPathEffect.reset(new SkDashPathEffect(vals, 2, SK_Scalar1, false));
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkPaint p;
this->setupPaint(&p);
p.setColor(SK_ColorBLACK);
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
p.setPathEffect(fPathEffect);
p.setAntiAlias(fDoAA);
SkPoint pts[2] = {
{ SkIntToScalar(10), 0 },
{ SkIntToScalar(640), 0 }
};
for (int i = 0; i < this->getLoops(); ++i) {
pts[0].fY = pts[1].fY = SkIntToScalar(i % 480);
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
}
}
private:
typedef SkBenchmark INHERITED;
};
// Want to test how we handle dashing when 99% of the dash is clipped out
class GiantDashBench : public SkBenchmark {
SkString fName;
SkScalar fStrokeWidth;
SkPoint fPts[2];
SkAutoTUnref<SkPathEffect> fPathEffect;
public:
enum LineType {
kHori_LineType,
kVert_LineType,
kDiag_LineType,
kLineTypeCount
};
static const char* LineTypeName(LineType lt) {
static const char* gNames[] = { "hori", "vert", "diag" };
SK_COMPILE_ASSERT(kLineTypeCount == SK_ARRAY_COUNT(gNames), names_wrong_size);
return gNames[lt];
}
GiantDashBench(LineType lt, SkScalar width) {
fName.printf("giantdashline_%s_%g", LineTypeName(lt), width);
fStrokeWidth = width;
// deliberately pick intervals that won't be caught by asPoints(), so
// we can test the filterPath code-path.
const SkScalar intervals[] = { 2, 1, 1, 1 };
fPathEffect.reset(new SkDashPathEffect(intervals,
SK_ARRAY_COUNT(intervals), 0));
SkScalar cx = 640 / 2; // center X
SkScalar cy = 480 / 2; // center Y
SkMatrix matrix;
switch (lt) {
case kHori_LineType:
matrix.setIdentity();
break;
case kVert_LineType:
matrix.setRotate(90, cx, cy);
break;
case kDiag_LineType:
matrix.setRotate(45, cx, cy);
break;
case kLineTypeCount:
// Not a real enum value.
break;
}
const SkScalar overshoot = 100*1000;
const SkPoint pts[2] = {
{ -overshoot, cy }, { 640 + overshoot, cy }
};
matrix.mapPoints(fPts, pts, 2);
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkPaint p;
this->setupPaint(&p);
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(fStrokeWidth);
p.setPathEffect(fPathEffect);
for (int i = 0; i < this->getLoops(); i++) {
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, p);
}
}
private:
typedef SkBenchmark INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 };
#define PARAM(array) array, SK_ARRAY_COUNT(array)
DEF_BENCH( return new DashBench(PARAM(gDots), 0); )
DEF_BENCH( return new DashBench(PARAM(gDots), 1); )
DEF_BENCH( return new DashBench(PARAM(gDots), 1, true); )
DEF_BENCH( return new DashBench(PARAM(gDots), 4); )
DEF_BENCH( return new MakeDashBench(make_poly, "poly"); )
DEF_BENCH( return new MakeDashBench(make_quad, "quad"); )
DEF_BENCH( return new MakeDashBench(make_cubic, "cubic"); )
DEF_BENCH( return new DashLineBench(0, false); )
DEF_BENCH( return new DashLineBench(SK_Scalar1, false); )
DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, false); )
DEF_BENCH( return new DashLineBench(0, true); )
DEF_BENCH( return new DashLineBench(SK_Scalar1, true); )
DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, true); )
DEF_BENCH( return new DrawPointsDashingBench(1, 1, false); )
DEF_BENCH( return new DrawPointsDashingBench(1, 1, true); )
DEF_BENCH( return new DrawPointsDashingBench(3, 1, false); )
DEF_BENCH( return new DrawPointsDashingBench(3, 1, true); )
DEF_BENCH( return new DrawPointsDashingBench(5, 5, false); )
DEF_BENCH( return new DrawPointsDashingBench(5, 5, true); )
/* Disable the GiantDashBench for Android devices until we can better control
* the memory usage. (https://code.google.com/p/skia/issues/detail?id=1430)
*/
#ifndef SK_BUILD_FOR_ANDROID
DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 0); )
DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 0); )
DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 0); )
// pass 2 to explicitly avoid any 1-is-the-same-as-hairline special casing
// hori_2 is just too slow to enable at the moment
DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 2); )
DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 2); )
DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 2); )
#endif
|