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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
|
/*
* 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 "SkColorPriv.h"
#include "SkPaint.h"
#include "SkRandom.h"
#include "SkShader.h"
#include "SkString.h"
#include "SkTArray.h"
enum Flags {
kStroke_Flag = 1 << 0,
kBig_Flag = 1 << 1
};
#define FLAGS00 Flags(0)
#define FLAGS01 Flags(kStroke_Flag)
#define FLAGS10 Flags(kBig_Flag)
#define FLAGS11 Flags(kStroke_Flag | kBig_Flag)
class PathBench : public SkBenchmark {
SkPaint fPaint;
SkString fName;
Flags fFlags;
public:
PathBench(Flags flags) : fFlags(flags) {
fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style :
SkPaint::kFill_Style);
fPaint.setStrokeWidth(SkIntToScalar(5));
fPaint.setStrokeJoin(SkPaint::kBevel_Join);
}
virtual void appendName(SkString*) = 0;
virtual void makePath(SkPath*) = 0;
virtual int complexity() { return 0; }
protected:
virtual const char* onGetName() SK_OVERRIDE {
fName.printf("path_%s_%s_",
fFlags & kStroke_Flag ? "stroke" : "fill",
fFlags & kBig_Flag ? "big" : "small");
this->appendName(&fName);
return fName.c_str();
}
virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
SkPaint paint(fPaint);
this->setupPaint(&paint);
SkPath path;
this->makePath(&path);
if (fFlags & kBig_Flag) {
SkMatrix m;
m.setScale(SkIntToScalar(10), SkIntToScalar(10));
path.transform(m);
}
int count = loops;
if (fFlags & kBig_Flag) {
count >>= 2;
}
count >>= (3 * complexity());
for (int i = 0; i < count; i++) {
canvas->drawPath(path, paint);
}
}
private:
typedef SkBenchmark INHERITED;
};
class TrianglePathBench : public PathBench {
public:
TrianglePathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("triangle");
}
virtual void makePath(SkPath* path) SK_OVERRIDE {
static const int gCoord[] = {
10, 10, 15, 5, 20, 20
};
path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
path->close();
}
private:
typedef PathBench INHERITED;
};
class RectPathBench : public PathBench {
public:
RectPathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("rect");
}
virtual void makePath(SkPath* path) SK_OVERRIDE {
SkRect r = { 10, 10, 20, 20 };
path->addRect(r);
}
private:
typedef PathBench INHERITED;
};
class OvalPathBench : public PathBench {
public:
OvalPathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("oval");
}
virtual void makePath(SkPath* path) SK_OVERRIDE {
SkRect r = { 10, 10, 23, 20 };
path->addOval(r);
}
private:
typedef PathBench INHERITED;
};
class CirclePathBench: public PathBench {
public:
CirclePathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("circle");
}
virtual void makePath(SkPath* path) SK_OVERRIDE {
path->addCircle(SkIntToScalar(20), SkIntToScalar(20),
SkIntToScalar(10));
}
private:
typedef PathBench INHERITED;
};
class SawToothPathBench : public PathBench {
public:
SawToothPathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("sawtooth");
}
virtual void makePath(SkPath* path) {
SkScalar x = SkIntToScalar(20);
SkScalar y = SkIntToScalar(20);
const SkScalar x0 = x;
const SkScalar dx = SK_Scalar1 * 5;
const SkScalar dy = SK_Scalar1 * 10;
path->moveTo(x, y);
for (int i = 0; i < 32; i++) {
x += dx;
path->lineTo(x, y - dy);
x += dx;
path->lineTo(x, y + dy);
}
path->lineTo(x, y + 2 * dy);
path->lineTo(x0, y + 2 * dy);
path->close();
}
virtual int complexity() SK_OVERRIDE { return 1; }
private:
typedef PathBench INHERITED;
};
class LongCurvedPathBench : public PathBench {
public:
LongCurvedPathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("long_curved");
}
virtual void makePath(SkPath* path) SK_OVERRIDE {
SkRandom rand (12);
int i;
for (i = 0; i < 100; i++) {
path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
}
path->close();
}
virtual int complexity() SK_OVERRIDE { return 2; }
private:
typedef PathBench INHERITED;
};
class LongLinePathBench : public PathBench {
public:
LongLinePathBench(Flags flags) : INHERITED(flags) {}
virtual void appendName(SkString* name) SK_OVERRIDE {
name->append("long_line");
}
virtual void makePath(SkPath* path) SK_OVERRIDE {
SkRandom rand;
path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
for (size_t i = 1; i < 100; i++) {
path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
}
}
virtual int complexity() SK_OVERRIDE { return 2; }
private:
typedef PathBench INHERITED;
};
class RandomPathBench : public SkBenchmark {
public:
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
return backend == kNonRendering_Backend;
}
protected:
void createData(int minVerbs,
int maxVerbs,
bool allowMoves = true,
SkRect* bounds = NULL) {
SkRect tempBounds;
if (NULL == bounds) {
tempBounds.setXYWH(0, 0, SK_Scalar1, SK_Scalar1);
bounds = &tempBounds;
}
fVerbCnts.reset(kNumVerbCnts);
for (int i = 0; i < kNumVerbCnts; ++i) {
fVerbCnts[i] = fRandom.nextRangeU(minVerbs, maxVerbs + 1);
}
fVerbs.reset(kNumVerbs);
for (int i = 0; i < kNumVerbs; ++i) {
do {
fVerbs[i] = static_cast<SkPath::Verb>(fRandom.nextULessThan(SkPath::kDone_Verb));
} while (!allowMoves && SkPath::kMove_Verb == fVerbs[i]);
}
fPoints.reset(kNumPoints);
for (int i = 0; i < kNumPoints; ++i) {
fPoints[i].set(fRandom.nextRangeScalar(bounds->fLeft, bounds->fRight),
fRandom.nextRangeScalar(bounds->fTop, bounds->fBottom));
}
this->restartMakingPaths();
}
void restartMakingPaths() {
fCurrPath = 0;
fCurrVerb = 0;
fCurrPoint = 0;
}
void makePath(SkPath* path) {
int vCount = fVerbCnts[(fCurrPath++) & (kNumVerbCnts - 1)];
for (int v = 0; v < vCount; ++v) {
int verb = fVerbs[(fCurrVerb++) & (kNumVerbs - 1)];
switch (verb) {
case SkPath::kMove_Verb:
path->moveTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
break;
case SkPath::kLine_Verb:
path->lineTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
break;
case SkPath::kQuad_Verb:
path->quadTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
fPoints[(fCurrPoint + 1) & (kNumPoints - 1)]);
fCurrPoint += 2;
break;
case SkPath::kConic_Verb:
path->conicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
fPoints[(fCurrPoint + 1) & (kNumPoints - 1)],
SK_ScalarHalf);
fCurrPoint += 2;
break;
case SkPath::kCubic_Verb:
path->cubicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
fPoints[(fCurrPoint + 1) & (kNumPoints - 1)],
fPoints[(fCurrPoint + 2) & (kNumPoints - 1)]);
fCurrPoint += 3;
break;
case SkPath::kClose_Verb:
path->close();
break;
default:
SkDEBUGFAIL("Unexpected path verb");
break;
}
}
}
void finishedMakingPaths() {
fVerbCnts.reset(0);
fVerbs.reset(0);
fPoints.reset(0);
}
private:
enum {
// these should all be pow 2
kNumVerbCnts = 1 << 5,
kNumVerbs = 1 << 5,
kNumPoints = 1 << 5,
};
SkAutoTArray<int> fVerbCnts;
SkAutoTArray<SkPath::Verb> fVerbs;
SkAutoTArray<SkPoint> fPoints;
int fCurrPath;
int fCurrVerb;
int fCurrPoint;
SkRandom fRandom;
typedef SkBenchmark INHERITED;
};
class PathCreateBench : public RandomPathBench {
public:
PathCreateBench() {
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return "path_create";
}
virtual void onPreDraw() SK_OVERRIDE {
this->createData(10, 100);
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
for (int i = 0; i < loops; ++i) {
if (i % 1000 == 0) {
fPath.reset(); // PathRef memory can grow without bound otherwise.
}
this->makePath(&fPath);
}
this->restartMakingPaths();
}
virtual void onPostDraw() SK_OVERRIDE {
this->finishedMakingPaths();
}
private:
SkPath fPath;
typedef RandomPathBench INHERITED;
};
class PathCopyBench : public RandomPathBench {
public:
PathCopyBench() {
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return "path_copy";
}
virtual void onPreDraw() SK_OVERRIDE {
this->createData(10, 100);
fPaths.reset(kPathCnt);
fCopies.reset(kPathCnt);
for (int i = 0; i < kPathCnt; ++i) {
this->makePath(&fPaths[i]);
}
this->finishedMakingPaths();
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
fCopies[idx] = fPaths[idx];
}
}
virtual void onPostDraw() SK_OVERRIDE {
fPaths.reset(0);
fCopies.reset(0);
}
private:
enum {
// must be a pow 2
kPathCnt = 1 << 5,
};
SkAutoTArray<SkPath> fPaths;
SkAutoTArray<SkPath> fCopies;
typedef RandomPathBench INHERITED;
};
class PathTransformBench : public RandomPathBench {
public:
PathTransformBench(bool inPlace) : fInPlace(inPlace) {}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fInPlace ? "path_transform_in_place" : "path_transform_copy";
}
virtual void onPreDraw() SK_OVERRIDE {
fMatrix.setScale(5 * SK_Scalar1, 6 * SK_Scalar1);
this->createData(10, 100);
fPaths.reset(kPathCnt);
for (int i = 0; i < kPathCnt; ++i) {
this->makePath(&fPaths[i]);
}
this->finishedMakingPaths();
if (!fInPlace) {
fTransformed.reset(kPathCnt);
}
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
if (fInPlace) {
for (int i = 0; i < loops; ++i) {
fPaths[i & (kPathCnt - 1)].transform(fMatrix);
}
} else {
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
fPaths[idx].transform(fMatrix, &fTransformed[idx]);
}
}
}
virtual void onPostDraw() SK_OVERRIDE {
fPaths.reset(0);
fTransformed.reset(0);
}
private:
enum {
// must be a pow 2
kPathCnt = 1 << 5,
};
SkAutoTArray<SkPath> fPaths;
SkAutoTArray<SkPath> fTransformed;
SkMatrix fMatrix;
bool fInPlace;
typedef RandomPathBench INHERITED;
};
class PathEqualityBench : public RandomPathBench {
public:
PathEqualityBench() { }
protected:
virtual const char* onGetName() SK_OVERRIDE {
return "path_equality_50%";
}
virtual void onPreDraw() SK_OVERRIDE {
fParity = 0;
this->createData(10, 100);
fPaths.reset(kPathCnt);
fCopies.reset(kPathCnt);
for (int i = 0; i < kPathCnt; ++i) {
this->makePath(&fPaths[i]);
fCopies[i] = fPaths[i];
}
this->finishedMakingPaths();
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
fParity ^= (fPaths[idx] == fCopies[idx & ~0x1]);
}
}
virtual void onPostDraw() SK_OVERRIDE {
fPaths.reset(0);
fCopies.reset(0);
}
private:
bool fParity; // attempt to keep compiler from optimizing out the ==
enum {
// must be a pow 2
kPathCnt = 1 << 5,
};
SkAutoTArray<SkPath> fPaths;
SkAutoTArray<SkPath> fCopies;
typedef RandomPathBench INHERITED;
};
class SkBench_AddPathTest : public RandomPathBench {
public:
enum AddType {
kAdd_AddType,
kAddTrans_AddType,
kAddMatrix_AddType,
kReverseAdd_AddType,
kReversePathTo_AddType,
};
SkBench_AddPathTest(AddType type) : fType(type) {
fMatrix.setRotate(60 * SK_Scalar1);
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
switch (fType) {
case kAdd_AddType:
return "path_add_path";
case kAddTrans_AddType:
return "path_add_path_trans";
case kAddMatrix_AddType:
return "path_add_path_matrix";
case kReverseAdd_AddType:
return "path_reverse_add_path";
case kReversePathTo_AddType:
return "path_reverse_path_to";
default:
SkDEBUGFAIL("Bad add type");
return "";
}
}
virtual void onPreDraw() SK_OVERRIDE {
// reversePathTo assumes a single contour path.
bool allowMoves = kReversePathTo_AddType != fType;
this->createData(10, 100, allowMoves);
fPaths0.reset(kPathCnt);
fPaths1.reset(kPathCnt);
for (int i = 0; i < kPathCnt; ++i) {
this->makePath(&fPaths0[i]);
this->makePath(&fPaths1[i]);
}
this->finishedMakingPaths();
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
switch (fType) {
case kAdd_AddType:
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
SkPath result = fPaths0[idx];
result.addPath(fPaths1[idx]);
}
break;
case kAddTrans_AddType:
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
SkPath result = fPaths0[idx];
result.addPath(fPaths1[idx], 2 * SK_Scalar1, 5 * SK_Scalar1);
}
break;
case kAddMatrix_AddType:
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
SkPath result = fPaths0[idx];
result.addPath(fPaths1[idx], fMatrix);
}
break;
case kReverseAdd_AddType:
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
SkPath result = fPaths0[idx];
result.reverseAddPath(fPaths1[idx]);
}
break;
case kReversePathTo_AddType:
for (int i = 0; i < loops; ++i) {
int idx = i & (kPathCnt - 1);
SkPath result = fPaths0[idx];
result.reversePathTo(fPaths1[idx]);
}
break;
}
}
virtual void onPostDraw() SK_OVERRIDE {
fPaths0.reset(0);
fPaths1.reset(0);
}
private:
AddType fType; // or reverseAddPath
enum {
// must be a pow 2
kPathCnt = 1 << 5,
};
SkAutoTArray<SkPath> fPaths0;
SkAutoTArray<SkPath> fPaths1;
SkMatrix fMatrix;
typedef RandomPathBench INHERITED;
};
class CirclesBench : public SkBenchmark {
protected:
SkString fName;
Flags fFlags;
public:
CirclesBench(Flags flags) : fFlags(flags) {
fName.printf("circles_%s", fFlags & kStroke_Flag ? "stroke" : "fill");
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setAntiAlias(true);
if (fFlags & kStroke_Flag) {
paint.setStyle(SkPaint::kStroke_Style);
}
SkRandom rand;
SkRect r;
for (int i = 0; i < loops; ++i) {
SkScalar radius = rand.nextUScalar1() * 3;
r.fLeft = rand.nextUScalar1() * 300;
r.fTop = rand.nextUScalar1() * 300;
r.fRight = r.fLeft + 2 * radius;
r.fBottom = r.fTop + 2 * radius;
if (fFlags & kStroke_Flag) {
paint.setStrokeWidth(rand.nextUScalar1() * 5.0f);
}
SkPath temp;
// mimic how Chrome does circles
temp.arcTo(r, 0, 0, false);
temp.addOval(r, SkPath::kCCW_Direction);
temp.arcTo(r, 360, 0, true);
temp.close();
canvas->drawPath(temp, paint);
}
}
private:
typedef SkBenchmark INHERITED;
};
// Chrome creates its own round rects with each corner possibly being different.
// In its "zero radius" incarnation it creates degenerate round rects.
// Note: PathTest::test_arb_round_rect_is_convex and
// test_arb_zero_rad_round_rect_is_rect perform almost exactly
// the same test (but with no drawing)
class ArbRoundRectBench : public SkBenchmark {
protected:
SkString fName;
public:
ArbRoundRectBench(bool zeroRad) : fZeroRad(zeroRad) {
if (zeroRad) {
fName.printf("zeroradroundrect");
} else {
fName.printf("arbroundrect");
}
}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
static void add_corner_arc(SkPath* path, const SkRect& rect,
SkScalar xIn, SkScalar yIn,
int startAngle)
{
SkScalar rx = SkMinScalar(rect.width(), xIn);
SkScalar ry = SkMinScalar(rect.height(), yIn);
SkRect arcRect;
arcRect.set(-rx, -ry, rx, ry);
switch (startAngle) {
case 0:
arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
break;
case 90:
arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
break;
case 180:
arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
break;
case 270:
arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
break;
default:
break;
}
path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
}
static void make_arb_round_rect(SkPath* path, const SkRect& r,
SkScalar xCorner, SkScalar yCorner) {
// we are lazy here and use the same x & y for each corner
add_corner_arc(path, r, xCorner, yCorner, 270);
add_corner_arc(path, r, xCorner, yCorner, 0);
add_corner_arc(path, r, xCorner, yCorner, 90);
add_corner_arc(path, r, xCorner, yCorner, 180);
path->close();
SkASSERT(path->isConvex());
}
virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
SkRandom rand;
SkRect r;
for (int i = 0; i < loops; ++i) {
SkPaint paint;
paint.setColor(0xff000000 | rand.nextU());
paint.setAntiAlias(true);
SkScalar size = rand.nextUScalar1() * 30;
if (size < SK_Scalar1) {
continue;
}
r.fLeft = rand.nextUScalar1() * 300;
r.fTop = rand.nextUScalar1() * 300;
r.fRight = r.fLeft + 2 * size;
r.fBottom = r.fTop + 2 * size;
SkPath temp;
if (fZeroRad) {
make_arb_round_rect(&temp, r, 0, 0);
SkASSERT(temp.isRect(NULL));
} else {
make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
}
canvas->drawPath(temp, paint);
}
}
private:
bool fZeroRad; // should 0 radius rounds rects be tested?
typedef SkBenchmark INHERITED;
};
class ConservativelyContainsBench : public SkBenchmark {
public:
enum Type {
kRect_Type,
kRoundRect_Type,
kOval_Type,
};
ConservativelyContainsBench(Type type) {
fParity = false;
fName = "conservatively_contains_";
switch (type) {
case kRect_Type:
fName.append("rect");
fPath.addRect(kBaseRect);
break;
case kRoundRect_Type:
fName.append("round_rect");
fPath.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1]);
break;
case kOval_Type:
fName.append("oval");
fPath.addOval(kBaseRect);
break;
}
}
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
return backend == kNonRendering_Backend;
}
private:
virtual const char* onGetName() SK_OVERRIDE {
return fName.c_str();
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
for (int i = 0; i < loops; ++i) {
const SkRect& rect = fQueryRects[i % kQueryRectCnt];
fParity = fParity != fPath.conservativelyContainsRect(rect);
}
}
virtual void onPreDraw() SK_OVERRIDE {
fQueryRects.setCount(kQueryRectCnt);
SkRandom rand;
for (int i = 0; i < kQueryRectCnt; ++i) {
SkSize size;
SkPoint xy;
size.fWidth = rand.nextRangeScalar(kQueryMin.fWidth, kQueryMax.fWidth);
size.fHeight = rand.nextRangeScalar(kQueryMin.fHeight, kQueryMax.fHeight);
xy.fX = rand.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth);
xy.fY = rand.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight);
fQueryRects[i] = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
}
}
virtual void onPostDraw() SK_OVERRIDE {
fQueryRects.setCount(0);
}
enum {
kQueryRectCnt = 400,
};
static const SkRect kBounds; // bounds for all random query rects
static const SkSize kQueryMin; // minimum query rect size, should be <= kQueryMax
static const SkSize kQueryMax; // max query rect size, should < kBounds
static const SkRect kBaseRect; // rect that is used to construct the path
static const SkScalar kRRRadii[2]; // x and y radii for round rect
SkString fName;
SkPath fPath;
bool fParity;
SkTDArray<SkRect> fQueryRects;
typedef SkBenchmark INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
#include "SkGeometry.h"
class ConicBench_Chop5 : public SkBenchmark {
SkConic fRQ;
public:
ConicBench_Chop5() {
fRQ.fPts[0].set(0, 0);
fRQ.fPts[1].set(100, 0);
fRQ.fPts[2].set(100, 100);
fRQ.fW = SkScalarCos(SK_ScalarPI/4);
}
private:
virtual const char* onGetName() SK_OVERRIDE {
return "ratquad-chop-0.5";
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
SkConic dst[2];
for (int i = 0; i < loops; ++i) {
fRQ.chopAt(0.5f, dst);
}
}
typedef SkBenchmark INHERITED;
};
class ConicBench_ChopHalf : public SkBenchmark {
SkConic fRQ;
public:
ConicBench_ChopHalf() {
fRQ.fPts[0].set(0, 0);
fRQ.fPts[1].set(100, 0);
fRQ.fPts[2].set(100, 100);
fRQ.fW = SkScalarCos(SK_ScalarPI/4);
}
private:
virtual const char* onGetName() SK_OVERRIDE {
return "ratquad-chop-half";
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
SkConic dst[2];
for (int i = 0; i < loops; ++i) {
fRQ.chop(dst);
}
}
typedef SkBenchmark INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
static void rand_conic(SkConic* conic, SkRandom& rand) {
for (int i = 0; i < 3; ++i) {
conic->fPts[i].set(rand.nextUScalar1() * 100, rand.nextUScalar1() * 100);
}
if (rand.nextUScalar1() > 0.5f) {
conic->fW = rand.nextUScalar1();
} else {
conic->fW = 1 + rand.nextUScalar1() * 4;
}
}
class ConicBench : public SkBenchmark {
public:
ConicBench() {
SkRandom rand;
for (int i = 0; i < CONICS; ++i) {
rand_conic(&fConics[i], rand);
}
}
virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
return backend == kNonRendering_Backend;
}
protected:
enum {
CONICS = 100
};
SkConic fConics[CONICS];
private:
typedef SkBenchmark INHERITED;
};
class ConicBench_ComputeError : public ConicBench {
public:
ConicBench_ComputeError() {}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return "conic-compute-error";
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
SkVector err;
for (int i = 0; i < loops; ++i) {
for (int j = 0; j < CONICS; ++j) {
fConics[j].computeAsQuadError(&err);
}
}
}
private:
typedef ConicBench INHERITED;
};
class ConicBench_asQuadTol : public ConicBench {
public:
ConicBench_asQuadTol() {}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return "conic-asQuadTol";
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
for (int i = 0; i < loops; ++i) {
for (int j = 0; j < CONICS; ++j) {
fConics[j].asQuadTol(SK_ScalarHalf);
}
}
}
private:
typedef ConicBench INHERITED;
};
class ConicBench_quadPow2 : public ConicBench {
public:
ConicBench_quadPow2() {}
protected:
virtual const char* onGetName() SK_OVERRIDE {
return "conic-quadPow2";
}
virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
for (int i = 0; i < loops; ++i) {
for (int j = 0; j < CONICS; ++j) {
fConics[j].computeQuadPOW2(SK_ScalarHalf);
}
}
}
private:
typedef ConicBench INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
const SkRect ConservativelyContainsBench::kBounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
const SkSize ConservativelyContainsBench::kQueryMin = SkSize::Make(SkIntToScalar(1), SkIntToScalar(1));
const SkSize ConservativelyContainsBench::kQueryMax = SkSize::Make(SkIntToScalar(40), SkIntToScalar(40));
const SkRect ConservativelyContainsBench::kBaseRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), SkIntToScalar(50), SkIntToScalar(50));
const SkScalar ConservativelyContainsBench::kRRRadii[2] = {SkIntToScalar(5), SkIntToScalar(10)};
DEF_BENCH( return new TrianglePathBench(FLAGS00); )
DEF_BENCH( return new TrianglePathBench(FLAGS01); )
DEF_BENCH( return new TrianglePathBench(FLAGS10); )
DEF_BENCH( return new TrianglePathBench(FLAGS11); )
DEF_BENCH( return new RectPathBench(FLAGS00); )
DEF_BENCH( return new RectPathBench(FLAGS01); )
DEF_BENCH( return new RectPathBench(FLAGS10); )
DEF_BENCH( return new RectPathBench(FLAGS11); )
DEF_BENCH( return new OvalPathBench(FLAGS00); )
DEF_BENCH( return new OvalPathBench(FLAGS01); )
DEF_BENCH( return new OvalPathBench(FLAGS10); )
DEF_BENCH( return new OvalPathBench(FLAGS11); )
DEF_BENCH( return new CirclePathBench(FLAGS00); )
DEF_BENCH( return new CirclePathBench(FLAGS01); )
DEF_BENCH( return new CirclePathBench(FLAGS10); )
DEF_BENCH( return new CirclePathBench(FLAGS11); )
DEF_BENCH( return new SawToothPathBench(FLAGS00); )
DEF_BENCH( return new SawToothPathBench(FLAGS01); )
DEF_BENCH( return new LongCurvedPathBench(FLAGS00); )
DEF_BENCH( return new LongCurvedPathBench(FLAGS01); )
DEF_BENCH( return new LongLinePathBench(FLAGS00); )
DEF_BENCH( return new LongLinePathBench(FLAGS01); )
DEF_BENCH( return new PathCreateBench(); )
DEF_BENCH( return new PathCopyBench(); )
DEF_BENCH( return new PathTransformBench(true); )
DEF_BENCH( return new PathTransformBench(false); )
DEF_BENCH( return new PathEqualityBench(); )
DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType); )
DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType); )
DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType); )
DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType); )
DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType); )
DEF_BENCH( return new CirclesBench(FLAGS00); )
DEF_BENCH( return new CirclesBench(FLAGS01); )
DEF_BENCH( return new ArbRoundRectBench(false); )
DEF_BENCH( return new ArbRoundRectBench(true); )
DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRect_Type); )
DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRoundRect_Type); )
DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kOval_Type); )
DEF_BENCH( return new ConicBench_Chop5() )
DEF_BENCH( return new ConicBench_ChopHalf() )
DEF_BENCH( return new ConicBench_ComputeError() )
DEF_BENCH( return new ConicBench_asQuadTol() )
DEF_BENCH( return new ConicBench_quadPow2() )
|