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