1 #include "SkBenchmark.h"
2 #include "SkCanvas.h"
3 #include "SkPaint.h"
4 #include "SkRandom.h"
5 #include "SkString.h"
6
7 class RectBench : public SkBenchmark {
8 public:
9 int fShift;
10 enum {
11 W = 640,
12 H = 480,
13 N = 300
14 };
15 SkRect fRects[N];
16 SkColor fColors[N];
17
RectBench(void * param,int shift)18 RectBench(void* param, int shift) : INHERITED(param), fShift(shift) {
19 SkRandom rand;
20 for (int i = 0; i < N; i++) {
21 int x = rand.nextU() % W;
22 int y = rand.nextU() % H;
23 int w = rand.nextU() % W;
24 int h = rand.nextU() % H;
25 w >>= shift;
26 h >>= shift;
27 x -= w/2;
28 y -= h/2;
29 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
30 SkIntToScalar(x+w), SkIntToScalar(y+h));
31 fColors[i] = rand.nextU() | 0xFF808080;
32 }
33 }
34
35 SkString fName;
computeName(const char root[])36 const char* computeName(const char root[]) {
37 fName.set(root);
38 fName.appendS32(fShift);
39 return fName.c_str();
40 }
41
42 protected:
drawThisRect(SkCanvas * c,const SkRect & r,const SkPaint & p)43 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
44 c->drawRect(r, p);
45 }
46
onGetName()47 virtual const char* onGetName() { return computeName("rects"); }
onDraw(SkCanvas * canvas)48 virtual void onDraw(SkCanvas* canvas) {
49 SkPaint paint;
50 for (int i = 0; i < N; i++) {
51 paint.setColor(fColors[i]);
52 this->setupPaint(&paint);
53 this->drawThisRect(canvas, fRects[i], paint);
54 }
55 }
56 private:
57 typedef SkBenchmark INHERITED;
58 };
59
60 class OvalBench : public RectBench {
61 public:
OvalBench(void * param,int shift)62 OvalBench(void* param, int shift) : RectBench(param, shift) {}
63 protected:
drawThisRect(SkCanvas * c,const SkRect & r,const SkPaint & p)64 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
65 c->drawOval(r, p);
66 }
onGetName()67 virtual const char* onGetName() { return computeName("ovals"); }
68 };
69
70 class RRectBench : public RectBench {
71 public:
RRectBench(void * param,int shift)72 RRectBench(void* param, int shift) : RectBench(param, shift) {}
73 protected:
drawThisRect(SkCanvas * c,const SkRect & r,const SkPaint & p)74 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
75 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
76 }
onGetName()77 virtual const char* onGetName() { return computeName("rrects"); }
78 };
79
80 class PointsBench : public RectBench {
81 public:
82 SkCanvas::PointMode fMode;
83 const char* fName;
84
PointsBench(void * param,SkCanvas::PointMode mode,const char * name)85 PointsBench(void* param, SkCanvas::PointMode mode, const char* name) :
86 RectBench(param, 2), fMode(mode) {
87 fName = name;
88 }
89
90 protected:
onDraw(SkCanvas * canvas)91 virtual void onDraw(SkCanvas* canvas) {
92 static const SkScalar gSizes[] = {
93 SkIntToScalar(7), 0
94 };
95
96 SkPaint paint;
97 paint.setStrokeCap(SkPaint::kRound_Cap);
98
99 for (size_t i = 0; i < SK_ARRAY_COUNT(gSizes); i++) {
100 paint.setStrokeWidth(gSizes[i]);
101 this->setupPaint(&paint);
102 canvas->drawPoints(fMode, N * 2,
103 reinterpret_cast<const SkPoint*>(fRects), paint);
104 paint.setColor(fColors[i]);
105 }
106 }
onGetName()107 virtual const char* onGetName() { return fName; }
108 };
109
RectFactory1(void * p)110 static SkBenchmark* RectFactory1(void* p) { return SkNEW_ARGS(RectBench, (p, 1)); }
RectFactory2(void * p)111 static SkBenchmark* RectFactory2(void* p) { return SkNEW_ARGS(RectBench, (p, 3)); }
OvalFactory1(void * p)112 static SkBenchmark* OvalFactory1(void* p) { return SkNEW_ARGS(OvalBench, (p, 1)); }
OvalFactory2(void * p)113 static SkBenchmark* OvalFactory2(void* p) { return SkNEW_ARGS(OvalBench, (p, 3)); }
RRectFactory1(void * p)114 static SkBenchmark* RRectFactory1(void* p) { return SkNEW_ARGS(RRectBench, (p, 1)); }
RRectFactory2(void * p)115 static SkBenchmark* RRectFactory2(void* p) { return SkNEW_ARGS(RRectBench, (p, 3)); }
PointsFactory(void * p)116 static SkBenchmark* PointsFactory(void* p) {
117 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPoints_PointMode, "points"));
118 }
LinesFactory(void * p)119 static SkBenchmark* LinesFactory(void* p) {
120 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kLines_PointMode, "lines"));
121 }
PolygonFactory(void * p)122 static SkBenchmark* PolygonFactory(void* p) {
123 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPolygon_PointMode, "polygon"));
124 }
125
126 static BenchRegistry gRectReg1(RectFactory1);
127 static BenchRegistry gRectReg2(RectFactory2);
128 static BenchRegistry gOvalReg1(OvalFactory1);
129 static BenchRegistry gOvalReg2(OvalFactory2);
130 static BenchRegistry gRRectReg1(RRectFactory1);
131 static BenchRegistry gRRectReg2(RRectFactory2);
132 static BenchRegistry gPointsReg(PointsFactory);
133 static BenchRegistry gLinesReg(LinesFactory);
134 static BenchRegistry gPolygonReg(PolygonFactory);
135
136