blob: 69fbd9bb49cdb15ba7dd4ad3ca68e206dec68b62 [file] [log] [blame]
reed@google.comb8c39172012-03-07 12:36:07 +00001#include "SkBenchmark.h"
2#include "SkColorPriv.h"
3#include "SkMatrix.h"
4#include "SkRandom.h"
5#include "SkString.h"
6#include "SkPaint.h"
7
8#define TILE(x, width) (((x) & 0xFFFF) * width >> 16)
9
10class InterpBench : public SkBenchmark {
11 enum {
12 kBuffer = 128,
13 kLoop = 20000
14 };
15 SkString fName;
16 int16_t fDst[kBuffer];
17 float fFx, fDx;
18public:
19 InterpBench(void* param, const char name[]) : INHERITED(param) {
20 fName.printf("interp_%s", name);
21 fFx = 3.3;
22 fDx = 0.1257;
23 }
24
25 virtual void performTest(int16_t dst[], float x, float dx, int count) = 0;
26
27protected:
28 virtual int mulLoopCount() const { return 1; }
29
30 virtual const char* onGetName() {
31 return fName.c_str();
32 }
33
34 virtual void onDraw(SkCanvas* canvas) {
35 int n = SkBENCHLOOP(kLoop * this->mulLoopCount());
36 for (int i = 0; i < n; i++) {
37 this->performTest(fDst, fFx, fDx, kBuffer);
38 }
39 }
40
41private:
42 typedef SkBenchmark INHERITED;
43};
44
45class Fixed16D16Interp : public InterpBench {
46public:
47 Fixed16D16Interp(void* param) : INHERITED(param, "16.16") {}
48
49protected:
50 virtual void performTest(int16_t dst[], float fx, float dx, int count) SK_OVERRIDE {
51 SkFixed curr = SkFloatToFixed(fx);
52 SkFixed step = SkFloatToFixed(dx);
53 for (int i = 0; i < count; i += 4) {
54 dst[i + 0] = TILE(curr, count); curr += step;
55 dst[i + 1] = TILE(curr, count); curr += step;
56 dst[i + 2] = TILE(curr, count); curr += step;
57 dst[i + 3] = TILE(curr, count); curr += step;
58 }
59 }
60private:
61 typedef InterpBench INHERITED;
62};
63
64class Fixed32D32Interp : public InterpBench {
65public:
66 Fixed32D32Interp(void* param) : INHERITED(param, "32.32") {}
67
68protected:
69 virtual void performTest(int16_t dst[], float fx, float dx, int count) SK_OVERRIDE {
70 int64_t curr = (int64_t)(fx * 65536 * 655536);
71 int64_t step = (int64_t)(dx * 65536 * 655536);
72 SkFixed tmp;
73 for (int i = 0; i < count; i += 4) {
74 tmp = curr >> 16; dst[i + 0] = TILE(tmp, count); curr += step;
75 tmp = curr >> 16; dst[i + 1] = TILE(tmp, count); curr += step;
76 tmp = curr >> 16; dst[i + 2] = TILE(tmp, count); curr += step;
77 tmp = curr >> 16; dst[i + 3] = TILE(tmp, count); curr += step;
78 }
79 }
80private:
81 typedef InterpBench INHERITED;
82};
83
84class Fixed16D48Interp : public InterpBench {
85public:
86 Fixed16D48Interp(void* param) : INHERITED(param, "16.48") {}
87
88protected:
89 virtual void performTest(int16_t dst[], float fx, float dx, int count) SK_OVERRIDE {
90 int64_t curr = (int64_t)(fx * 65536 * 655536 * 65536);
91 int64_t step = (int64_t)(dx * 65536 * 655536 * 65536);
92 SkFixed tmp;
93 for (int i = 0; i < count; i += 4) {
94 tmp = curr >> 32; dst[i + 0] = TILE(tmp, count); curr += step;
95 tmp = curr >> 32; dst[i + 1] = TILE(tmp, count); curr += step;
96 tmp = curr >> 32; dst[i + 2] = TILE(tmp, count); curr += step;
97 tmp = curr >> 32; dst[i + 3] = TILE(tmp, count); curr += step;
98 }
99 }
100private:
101 typedef InterpBench INHERITED;
102};
103
104class FloatInterp : public InterpBench {
105public:
106 FloatInterp(void* param) : INHERITED(param, "float") {}
107
108protected:
109 virtual void performTest(int16_t dst[], float fx, float dx, int count) SK_OVERRIDE {
110 SkFixed tmp;
111 for (int i = 0; i < count; i += 4) {
112 tmp = SkFloatToFixed(fx); dst[i + 0] = TILE(tmp, count); fx += dx;
113 tmp = SkFloatToFixed(fx); dst[i + 1] = TILE(tmp, count); fx += dx;
114 tmp = SkFloatToFixed(fx); dst[i + 2] = TILE(tmp, count); fx += dx;
115 tmp = SkFloatToFixed(fx); dst[i + 3] = TILE(tmp, count); fx += dx;
116 }
117 }
118private:
119 typedef InterpBench INHERITED;
120};
121
122class DoubleInterp : public InterpBench {
123public:
124 DoubleInterp(void* param) : INHERITED(param, "double") {}
125
126protected:
127 virtual void performTest(int16_t dst[], float fx, float dx, int count) SK_OVERRIDE {
128 double ffx = fx;
129 double ddx = dx;
130 SkFixed tmp;
131 for (int i = 0; i < count; i += 4) {
132 tmp = SkDoubleToFixed(ffx); dst[i + 0] = TILE(tmp, count); ffx += ddx;
133 tmp = SkDoubleToFixed(ffx); dst[i + 1] = TILE(tmp, count); ffx += ddx;
134 tmp = SkDoubleToFixed(ffx); dst[i + 2] = TILE(tmp, count); ffx += ddx;
135 tmp = SkDoubleToFixed(ffx); dst[i + 3] = TILE(tmp, count); ffx += ddx;
136 }
137 }
138private:
139 typedef InterpBench INHERITED;
140};
141
142///////////////////////////////////////////////////////////////////////////////
143
144static SkBenchmark* M0(void* p) { return new Fixed16D16Interp(p); }
145static SkBenchmark* M1(void* p) { return new Fixed32D32Interp(p); }
146static SkBenchmark* M2(void* p) { return new Fixed16D48Interp(p); }
147static SkBenchmark* M3(void* p) { return new FloatInterp(p); }
148static SkBenchmark* M4(void* p) { return new DoubleInterp(p); }
149
150static BenchRegistry gReg0(M0);
151static BenchRegistry gReg1(M1);
152static BenchRegistry gReg2(M2);
153static BenchRegistry gReg3(M3);
154static BenchRegistry gReg4(M4);
155