reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkBitmap.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkColorPriv.h" |
| 5 | #include "SkPaint.h" |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 6 | #include "SkRandom.h" |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 7 | #include "SkShader.h" |
| 8 | #include "SkString.h" |
| 9 | |
| 10 | enum Flags { |
| 11 | kStroke_Flag = 1 << 0, |
| 12 | kBig_Flag = 1 << 1 |
| 13 | }; |
| 14 | |
| 15 | #define FLAGS00 Flags(0) |
| 16 | #define FLAGS01 Flags(kStroke_Flag) |
| 17 | #define FLAGS10 Flags(kBig_Flag) |
| 18 | #define FLAGS11 Flags(kStroke_Flag | kBig_Flag) |
| 19 | |
| 20 | class PathBench : public SkBenchmark { |
| 21 | SkPaint fPaint; |
| 22 | SkString fName; |
| 23 | Flags fFlags; |
| 24 | enum { N = 1000 }; |
| 25 | public: |
| 26 | PathBench(void* param, Flags flags) : INHERITED(param), fFlags(flags) { |
| 27 | fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style : |
| 28 | SkPaint::kFill_Style); |
| 29 | fPaint.setStrokeWidth(SkIntToScalar(5)); |
| 30 | fPaint.setStrokeJoin(SkPaint::kBevel_Join); |
| 31 | } |
| 32 | |
| 33 | virtual void appendName(SkString*) = 0; |
| 34 | virtual void makePath(SkPath*) = 0; |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 35 | virtual int complexity() { return 0; } |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 36 | |
| 37 | protected: |
| 38 | virtual const char* onGetName() { |
| 39 | fName.printf("path_%s_%s_", |
| 40 | fFlags & kStroke_Flag ? "stroke" : "fill", |
| 41 | fFlags & kBig_Flag ? "big" : "small"); |
| 42 | this->appendName(&fName); |
| 43 | return fName.c_str(); |
| 44 | } |
| 45 | |
| 46 | virtual void onDraw(SkCanvas* canvas) { |
| 47 | SkPaint paint(fPaint); |
| 48 | this->setupPaint(&paint); |
| 49 | |
| 50 | SkPath path; |
| 51 | this->makePath(&path); |
| 52 | if (fFlags & kBig_Flag) { |
| 53 | SkMatrix m; |
| 54 | m.setScale(SkIntToScalar(10), SkIntToScalar(10)); |
| 55 | path.transform(m); |
| 56 | } |
| 57 | |
| 58 | int count = N; |
| 59 | if (fFlags & kBig_Flag) { |
| 60 | count >>= 2; |
| 61 | } |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 62 | count >>= (3 * complexity()); |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 63 | |
| 64 | for (int i = 0; i < count; i++) { |
| 65 | canvas->drawPath(path, paint); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | typedef SkBenchmark INHERITED; |
| 71 | }; |
| 72 | |
| 73 | class TrianglePathBench : public PathBench { |
| 74 | public: |
| 75 | TrianglePathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
| 76 | |
| 77 | virtual void appendName(SkString* name) { |
| 78 | name->append("triangle"); |
| 79 | } |
| 80 | virtual void makePath(SkPath* path) { |
| 81 | static const int gCoord[] = { |
| 82 | 10, 10, 15, 5, 20, 20 |
| 83 | }; |
| 84 | path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1])); |
| 85 | path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3])); |
| 86 | path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5])); |
| 87 | path->close(); |
| 88 | } |
| 89 | private: |
| 90 | typedef PathBench INHERITED; |
| 91 | }; |
| 92 | |
| 93 | class RectPathBench : public PathBench { |
| 94 | public: |
| 95 | RectPathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
| 96 | |
| 97 | virtual void appendName(SkString* name) { |
| 98 | name->append("rect"); |
| 99 | } |
| 100 | virtual void makePath(SkPath* path) { |
| 101 | SkRect r = { 10, 10, 20, 20 }; |
| 102 | path->addRect(r); |
| 103 | } |
| 104 | private: |
| 105 | typedef PathBench INHERITED; |
| 106 | }; |
| 107 | |
| 108 | class OvalPathBench : public PathBench { |
| 109 | public: |
| 110 | OvalPathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
| 111 | |
| 112 | virtual void appendName(SkString* name) { |
| 113 | name->append("oval"); |
| 114 | } |
| 115 | virtual void makePath(SkPath* path) { |
| 116 | SkRect r = { 10, 10, 20, 20 }; |
| 117 | path->addOval(r); |
| 118 | } |
| 119 | private: |
| 120 | typedef PathBench INHERITED; |
| 121 | }; |
| 122 | |
| 123 | class SawToothPathBench : public PathBench { |
| 124 | public: |
| 125 | SawToothPathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
| 126 | |
| 127 | virtual void appendName(SkString* name) { |
| 128 | name->append("sawtooth"); |
| 129 | } |
| 130 | virtual void makePath(SkPath* path) { |
| 131 | SkScalar x = SkIntToScalar(20); |
| 132 | SkScalar y = SkIntToScalar(20); |
| 133 | const SkScalar x0 = x; |
| 134 | const SkScalar dx = SK_Scalar1 * 5; |
| 135 | const SkScalar dy = SK_Scalar1 * 10; |
| 136 | |
| 137 | path->moveTo(x, y); |
| 138 | for (int i = 0; i < 32; i++) { |
| 139 | x += dx; |
| 140 | path->lineTo(x, y - dy); |
| 141 | x += dx; |
| 142 | path->lineTo(x, y + dy); |
| 143 | } |
| 144 | path->lineTo(x, y + 2 * dy); |
| 145 | path->lineTo(x0, y + 2 * dy); |
| 146 | path->close(); |
| 147 | } |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 148 | virtual int complexity() { return 1; } |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 149 | private: |
| 150 | typedef PathBench INHERITED; |
| 151 | }; |
| 152 | |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 153 | class LongCurvedPathBench : public PathBench { |
| 154 | public: |
| 155 | LongCurvedPathBench(void * param, Flags flags) |
| 156 | : INHERITED(param, flags) { |
| 157 | } |
| 158 | |
| 159 | virtual void appendName(SkString* name) { |
| 160 | name->append("long_curved"); |
| 161 | } |
| 162 | virtual void makePath(SkPath* path) { |
| 163 | SkRandom rand (12); |
| 164 | int i; |
| 165 | for (i = 0; i < 100; i++) { |
| 166 | path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)), |
| 167 | SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)), |
| 168 | SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)), |
| 169 | SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480))); |
| 170 | } |
| 171 | path->close(); |
| 172 | } |
| 173 | virtual int complexity() { return 2; } |
| 174 | private: |
| 175 | typedef PathBench INHERITED; |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 180 | static SkBenchmark* FactT00(void* p) { return new TrianglePathBench(p, FLAGS00); } |
| 181 | static SkBenchmark* FactT01(void* p) { return new TrianglePathBench(p, FLAGS01); } |
| 182 | static SkBenchmark* FactT10(void* p) { return new TrianglePathBench(p, FLAGS10); } |
| 183 | static SkBenchmark* FactT11(void* p) { return new TrianglePathBench(p, FLAGS11); } |
| 184 | |
| 185 | static SkBenchmark* FactR00(void* p) { return new RectPathBench(p, FLAGS00); } |
| 186 | static SkBenchmark* FactR01(void* p) { return new RectPathBench(p, FLAGS01); } |
| 187 | static SkBenchmark* FactR10(void* p) { return new RectPathBench(p, FLAGS10); } |
| 188 | static SkBenchmark* FactR11(void* p) { return new RectPathBench(p, FLAGS11); } |
| 189 | |
| 190 | static SkBenchmark* FactO00(void* p) { return new OvalPathBench(p, FLAGS00); } |
| 191 | static SkBenchmark* FactO01(void* p) { return new OvalPathBench(p, FLAGS01); } |
| 192 | static SkBenchmark* FactO10(void* p) { return new OvalPathBench(p, FLAGS10); } |
| 193 | static SkBenchmark* FactO11(void* p) { return new OvalPathBench(p, FLAGS11); } |
| 194 | |
| 195 | static SkBenchmark* FactS00(void* p) { return new SawToothPathBench(p, FLAGS00); } |
| 196 | static SkBenchmark* FactS01(void* p) { return new SawToothPathBench(p, FLAGS01); } |
| 197 | |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 198 | static SkBenchmark* FactLC00(void* p) { |
| 199 | return new LongCurvedPathBench(p, FLAGS00); |
| 200 | } |
| 201 | static SkBenchmark* FactLC01(void* p) { |
| 202 | return new LongCurvedPathBench(p, FLAGS01); |
| 203 | } |
| 204 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 205 | static BenchRegistry gRegT00(FactT00); |
| 206 | static BenchRegistry gRegT01(FactT01); |
| 207 | static BenchRegistry gRegT10(FactT10); |
| 208 | static BenchRegistry gRegT11(FactT11); |
| 209 | |
| 210 | static BenchRegistry gRegR00(FactR00); |
| 211 | static BenchRegistry gRegR01(FactR01); |
| 212 | static BenchRegistry gRegR10(FactR10); |
| 213 | static BenchRegistry gRegR11(FactR11); |
| 214 | |
| 215 | static BenchRegistry gRegO00(FactO00); |
| 216 | static BenchRegistry gRegO01(FactO01); |
| 217 | static BenchRegistry gRegO10(FactO10); |
| 218 | static BenchRegistry gRegO11(FactO11); |
| 219 | |
| 220 | static BenchRegistry gRegS00(FactS00); |
| 221 | static BenchRegistry gRegS01(FactS01); |
| 222 | |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 223 | static BenchRegistry gRegLC00(FactLC00); |
| 224 | static BenchRegistry gRegLC01(FactLC01); |
| 225 | |