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