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